I need to use "db.RunCommand" to get an item from an array embedded into a collection. I am just starting MongoDB and I am having difficulty looking into the MongoDB documentation since i can't find much examples using directly the "RunCommand" method.
So if i have this example
[
{
"Brand": {
"Name": "Brand name 1",
"Products": [
{
"Product": "Product 1",
"Price": 20.33
},
// i need to get only the following object
{
"Product": "Product 2",
"Price": 10.33
},
{
"Product": "Product 2",
"Price": 15.33
}
]
}
}
]
So how can i construct the query into the "RunCommand(--query here--)" to get only "Product 2" ?
So the result would be
{
"Product": "Product 2",
"Price": 10.33
}
If is needed, i am using C# .net Core with MongoDB Driver
db.runCommand({
aggregate: "your_collection",
"pipeline": [
{$match: {"_id": ObjectId("5fcd2d3b08c6590a0cd04630")}},
{$project: {
_id: 0, "products": {"$arrayElemAt": ["$a.Brand.Products", 0]}}},
{$project: {"products": {"$arrayElemAt": ["$products", 1]}}}],
cursor: {}})
Related
I have a class called ConnectionsBank that has a list of Connections and a list of Connection Categories. The Connections class has a collection of PhoneNumber and ConnectionCategories. PhoneNumber class has a PhoneNumberType.
In my controller I have a Get that uses AutoMapper 7:
var result = await ControllerRepository.GetConnectionManagerAsync(connectionManagerdID);
return Ok(Mapper.Map<ConnectionManagerViewModel>(result));
The following is the return JSON:
{
"id": 1,
"jobSeekerID": 1,
"connections": [
{
"id": 1,
"completeName": {
"firstName": "Mary",
"lastName": "Jane"
},
"address": {
"street": "1 Main Street",
"city": "Rushville",
"state": "IN",
"country": "USA",
"zipCode": "99999"
},
"phoneNumbers": [
{
"phNumber": "317-922-8823",
"phoneType": null
}
],
"categories": null
}
],
"connectionCategories": [
{
"categoryName": "FIRST CONNECTION CATEGORY"
},
{
"categoryName": "SECOND CONNECTION CATEGORY"
}
]
}
Notice that "categories" and "phoneType" are null. The database has the data in the tables. Why would this happen? I am using EntityFrameworkCore 3.1.4.
I am working on a WEB API and need to return json result in this format:
{
"responseID": "b806d829-1d9c-4c69-8d7a-576ffc4de555",
"orders": [
{
"id": order1,
"status": "Issued",
"statusDate": "2017-04-05T10:00:56.1549453+00:00",
"amount": 100000,
"quantity": 10,
"fee": 99,
"comments": "some comments"
},
{
"id": order2,
"status": "Declined",
"statusDate": "2017-04-05T10:00:56.1549453+00:00",
}
]
}
Can we prepare a json object like this where the objects within array has different attributes like in the above example order1 has 7 attributes but order2 has only 3.
My understanding is that the only way to achieve the desired results is to change the format of json object as below:
{
"responseID": "b806d829-1d9c-4c69-8d7a-576ffc4de555",
"issuesOrders": [
{
"id": order1,
"status": "Issued",
"statusDate": "2017-04-05T10:00:56.1549453+00:00",
"amount": 100000,
"quantity": 10,
"fee": 99,
"comments": "some comments"
} ],
"declinedOrders": [
{
"id": order2,
"status": "Declined",
"statusDate": "2017-04-05T10:00:56.1549453+00:00",
}
]
}
Is my understanding is correct?
In Windows Phone App.. I am parsing JSON Data By (get,set) method
I got another way of processing JSON data from this URL
In the part of this site (http://www.dfg-team.com/json-feeds-on-windows-phone/?lang=en)
Is used two techniques to parse json data..
in that I want to use this. Key word for processing JSON data ..
But My app have data Have a Lot of Json data which contains lot of JSON Arrays and JSON Objects. But its not a Problem in that I have main problem is My json contains Json array inside json object or json object inside the json array so at here we got problem.
This is My JSon data I want to parse these fields.. using this. method
{
"returnCode": "success",
"SData": {
"results": [
{
"wdetails": [
{
"noffers": [],
"offers_count": 0,
"name": "yahoo.com",
"recentoffers_count": 0,
"sku": "30072826",
"url": "http://www.yahoo.com"
},
{
"noffers": [
{
"id": "3aggEYoyaso",
"price": "179.99",
"firstrecorded_at": 13700,
"lastrecorded_at": 13700,
"seller": "google",
"availability": "In stock, ships same Business Day.",
"currency": "USD"
}
],
"offers_count": 1,
"name": "frys.com",
"recentoffers_count": 1,
"sku": "789",
"url": "http://www.google.com"
},
{
"listprice_currency": "USD",
"noffers": [
{
"id": "2SlCKQKm",
"price": "192.37",
"firstrecorded_at": 111,
"lastrecorded_at": 111,
"seller": "Amazon",
"availability": "13 In Stock",
"currency": "USD"
}
],
"offers_count": 1,
"name": "amazon.com",
"listprice": "288.56",
"recentoffers_count": 1,
"sku": "1084089",
"url": "http://www.amazon.com"
}
],
"model": "GGGG",
"weight": "771107.03",
"price_currency": "USD",
"gtins": [
"00839294"
],
"mpn": "GGGG",
"cat_id": "25552",
"height": "98.81",
"features": {
"Product Type - General": "Tablet",
"Height (in.)": "1.70"
},
"length": "416.56",
"geo": [
"usa"
],
"width": "267.97",
"category": "Tablets",
"price": "179.99",
"updated_at": 138,
"color": "Black",
"manufacturer": "google",
"images_total": 1,
"images": [
"https://evbdn.eventbrite.com/s3-s3/eventlogos/1832816/google.png"
],
"brand": "Dell",
"offers_total": 180
}
],
"total_results_count": 1,
"results_count": 1,
"code": "OK",
"offset": 0
}
}
I want to parse this Using this. in C#.
In Android I done this By hash map method, in Windows Phone this. is similar to is hash map so I am using this..
I am editing my previous answer :-
JObject json = JObject.Parse(yourJson);
if you need results to be parsed
this.ClassOfResult= json ["SData"]["results"];
if you want the 'wdetails' in the Json to get parsed you can try this
this.parentclass.wdetailsClass= json ["SData"]["results"]["wdetails"];
Hope that this will give you some idea.
I am looking for a good way to feed a d3.js bubble chart with data from my MVC application. For example the standard bubble chart expects nested data in the form:
{
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{
"name": "CNN",
"size": 3938
}
]
},
{
"name": "graph",
"children": [
{
"name": "MTV",
"size": 3534
}
]
}
]
}
]
}
What I have on the server side is this linq query to a SQL database:
var results = from a in db.Durations
where a.Category == "watch"
group a by a.Description
into g
select new
{
name = g.Key,
size = g.Select(d => new{d.Begin, d.End}).Sum(d => SqlFunctions.DateDiff("hh", d.Begin, d.End))
};
return Json(results, JsonRequestBehavior.AllowGet);
The query result, parsed as Json, looks like this:
[{"name":"CNN","size":1950},{"name":"MTV","size":1680}]
I've got stuck in the head on what would be a good way to achieve the correct formatting and to create the nested structure from my query results..
server-side, using anonymous types
server-side, adjusting the linq-query
client-side, using d3.js nest
use a simpler bubble model since for my purpose, the nested
structure with children is not really needed
something totally different and much much cooler than 1-4
Thank you for any input.
Replace your return statement with the following one.
return Json(new
{
name = "Sites",
children = results
},
JsonRequestBehavior.AllowGet);
That will give you the following:
{
"name": "Sites",
"children": [
{
"name": "CNN",
"size": 1950
},
{
"name": "MTV",
"size": 1680
}
]
}
To serve as an example, suppose each website had an additional string Type property, with values such as "News" or "Music". Then you could do the following.
return Json(new
{
name = "Sites",
children = results.GroupBy(site => site.Type).Select(group => new
{
name = group.Key,
children = group
}
},
JsonRequestBehavior.AllowGet);
This would give you something like the following.
{
"name": "Sites",
"children": [
{
"name": "News",
"children": [
{
"name": "CNN",
"size": 1950
},
{
"name": "The Verge",
"size": 1600
}
]
},
{
"name": "Music",
"children": [
{
"name": "MTV",
"size": 1680
},
{
"name": "Pandora",
"size": 2000
}
]
}
]
}
This question already has answers here:
Parsing JSON using Json.net
(5 answers)
Closed 3 years ago.
I am using Newtonsoft Json library to parse json but i don't know how to use it. I parsed the string using JObject. When i output the value of JObject instance in immediate window i get this :-
json
{
"data": [
{
"id": "id",
"from": {
"name": "name",
"id": "someotherid"
},
"name": "pic",
"description": "desc",
"link": "linktosite",
"privacy": "everyone",
"count": 1,
"type": "normal",
"created_time": "2010-10-22T14:54:32+0000",
"updated_time": "2010-10-22T14:55:41+0000"
},
{
"id": "id2",
"from": {
"name": "name",
"id": "someotherid"
},
"name": "Profile Pictures",
"link": "link",
"privacy": "everyone",
"count": 6,
"type": "profile",
"created_time": "2010-10-12T14:27:58+0000",
"updated_time": "2011-01-01T18:38:14+0000"
},
{
"id": "id3",
"from": {
"name": "name",
"id": "829741958"
},
"name": "T",
"link": "link",
"privacy": "everyone",
"count": 5,
"type": "normal",
"created_time": "2010-05-01T03:03:39+0000",
"updated_time": "2010-05-01T03:19:13+0000",
"comments": {
"data": [
{
"id": "id",
"from": null,
"message": "message",
"created_time": "2010-08-28T18:27:10+0000",
"likes": 1
}
]
}
}
],
"paging": {
"previous": "paginglink",
"next": "otherpaginglink"
}
}
Count: 2
Type: Object
What should i do further to have the values from this jobject?
I personally prefer the JavaScriptSerializer for use with JSON in the .NET environment. By default it will return a Dictionary result, but can be used to parse in to a custom object (or you could make use of the dynamic datatype).
Some other posts on SO with JSON & JavaScript Serializer as topic