C# MongoDB - Filtering nested array data - c#

i am new on MongoDB and i am trying to use it in C# context. Let´s say, i have documents like this:
[
{
"Number": "2140007529",
"Name": "ABC",
"IsInactive": true,
"EntryList": [
{
"Timestamp": "2022-06-01T14:00:00.000+00:00",
"Value": 21564.0
},
{
"Timestamp": "2022-07-01T21:31:00.000+00:00",
"Value": 21568.0
},
{
"Timestamp": "2022-08-02T21:21:00.000+00:00",
"Value": 21581.642
},
{
"Timestamp": "2022-09-02T15:42:00.000+00:00",
"Value": 21593.551
},
{
"Timestamp": "2022-09-26T13:00:00.000+00:00",
"Value": 21603
}
]
},
{
"Number": "2220000784",
"Name": "XYZ",
"IsInactive": false,
"EntryList": [
{
"Timestamp": "2022-09-26T13:00:00.000+00:00",
"Value": 0.0
},
{
"Timestamp": "2022-10-01T08:49:00.000+00:00",
"Value": 5.274
},
{
"Timestamp": "2022-11-01T09:56:00.000+00:00",
"Value": 76.753
},
{
"Timestamp": "2022-12-01T19:43:00.000+00:00",
"Value": 244.877
},
{
"Timestamp": "2023-01-01T11:54:00.000+00:00",
"Value": 528.56
},
{
"Timestamp": "2023-02-01T17:21:00.000+00:00",
"Value": 802.264
}
]
}
]
I want to get the document where the IsInactive flag is false. But for the EntryList there should be returned entries greater than Timestamp 2022-12-31 only.I should look like this:
{
"Number": "2220000784",
"Name": "XYZ",
"IsInactive": false,
"EntryList": [
{
"Timestamp": "2023-01-01T11:54:00.000+00:00",
"Value": 528.56
},
{
"Timestamp": "2023-02-01T17:21:00.000+00:00",
"Value": 802.264
}
]
}
So, here is my question. How can i filter nested arrays in return value with C#. Thanks for help!
I tried to get the result with aggregate function of MongoDB in MongoDB Compass. I got it work with but not in C#.

I think you are looking for a query similar to this one.
So you can try something like this code:
var desiredTimestamp = new DateTime(2022, 12, 31);
var results = collection.AsQueryable()
.Where(x => x.IsInactive == false && x.EntryList.Any(e => e.Timestamp >= desiredTimestamp))
.Select(obj => new
{
Number = obj.Number,
Name = obj.Name,
IsInactive = obj.IsInactive,
EntryList = obj.EntryList
.Where(e => e.Timestamp >= desiredTimestamp)
.ToList()
}).ToList()
Note that I'm assuming your Timestamp is a date type, otherwise you can't compare date and string.

Related

How do I get UNIQUE categories from all documents in CosmosDB?

I have millions of documents in CosmosDB using SQL API, and I need to find the unique categories from all documents.
The documents looks like follows, you can see the categories array just under the description, I dont care in what order they are I just need to know all the unique ones from all documents in the collection, I need this so that later on I can create queries on the categories but thats a later question I first need to get them all out so I know what all the possible options are, but I am unable to figure out the query to do this so that I get only the category names.
{
"id": "56d934d3-90bf-4f5a-b602-e515fefa599f",
"_id": "5bf6705f9568cf00013cd13c",
"vendor": "XXX",
"updatedAt": "2018-11-23T03:55:30.044Z",
"locales": [
{
"title": "Cold shoulder t-shirt",
"description": "Because collar bones. Trending cold shoulder t-shirt in 100% organic cotton. Classic, wide and boxy t-shirt fit with cut-out details. In black, because black tees and fashion are like this (insert friendly hand gesture). This style is online exclusive.",
"categories": [
"Women",
"clothing",
"tops"
],
"brand": null,
"images": [
"https://lp.xxx.com/app002prod?set=source[01_0659881_001_102],type[ECOMLOOK],device[hdpi],quality[80],ImageVersion[2018081]&call=url[file:/product/main]",
"https://lp.xxx.com/app002prod?set=source[01_0659881_001_203],type[ECOMLOOK],device[hdpi],quality[80],ImageVersion[2018081]&call=url[file:/product/main]",
"https://lp.xxx.com/app002prod?set=source[01_0659881_001_301],type[ECOMLOOK],device[hdpi],quality[80],ImageVersion[2018081]&call=url[file:/product/main]",
"https://lp.xxx.com/app002prod?set=source[02_0659881_001_101],type[PRODUCT],device[hdpi],quality[80],ImageVersion[1.0]&call=url[file:/product/main]"
],
"country": "SE",
"currency": "SEK",
"language": "en",
"variants": [
{
"artno": "0659881001",
"urls": [
"https://click.linksynergy.com/link?id=INtcw3sexSw&offerid=491018&type=2&murl=https%3A%2F%2Fwww.xxx.com%2Fen_sek%2Fclothing%2Ftops%2Fproduct.cold-shoulder-t-shirt-black-magic.0659881001.html"
],
"price": 80,
"stock": 0,
"attributes": {
"size": "XXS",
"color": "Black magic"
}
},
{
"artno": "xxx",
"urls": [
"https://click.linksynergy.com/link?id=INtcw3sexSw&offerid=491018&type=2&murl=https%3A%2F%2Fwww.xxx.com%2Fen_sek%2Fclothing%2Ftops%2Fproduct.cold-shoulder-t-shirt-black-magic.0659881001.html"
],
"price": 80,
"stock": 0,
"attributes": {
"size": "XS",
"color": "Black magic"
}
},
{
"artno": "0659881001",
"urls": [
"https://click.linksynergy.com/link?id=INtcw3sexSw&offerid=491018&type=2&murl=https%3A%2F%2Fwww.xxx.com%2Fen_sek%2Fclothing%2Ftops%2Fproduct.cold-shoulder-t-shirt-black-magic.0659881001.html"
],
"price": 80,
"stock": 0,
"attributes": {
"size": "XL",
"color": "Black magic"
}
},
{
"artno": "0659881001",
"urls": [
"https://click.linksynergy.com/link?id=INtcw3sexSw&offerid=491018&type=2&murl=https%3A%2F%2Fwww.xxx.com%2Fen_sek%2Fclothing%2Ftops%2Fproduct.cold-shoulder-t-shirt-black-magic.0659881001.html"
],
"price": 80,
"stock": 0,
"attributes": {
"size": "S",
"color": "Black magic"
}
},
{
"artno": "0659881001",
"urls": [
"https://click.linksynergy.com/link?id=INtcw3sexSw&offerid=491018&type=2&murl=https%3A%2F%2Fwww.xxx.com%2Fen_sek%2Fclothing%2Ftops%2Fproduct.cold-shoulder-t-shirt-black-magic.0659881001.html"
],
"price": 80,
"stock": 1,
"attributes": {
"size": "M",
"color": "Black magic"
}
},
{
"artno": "0659881001",
"urls": [
"https://click.linksynergy.com/link?id=INtcw3sexSw&offerid=491018&type=2&murl=https%3A%2F%2Fwww.xxx.com%2Fen_sek%2Fclothing%2Ftops%2Fproduct.cold-shoulder-t-shirt-black-magic.0659881001.html"
],
"price": 80,
"stock": 0,
"attributes": {
"size": "L",
"color": "Black magic"
}
}
]
}
],
"_rid": "QEwcALNbIz8GAAAAAAAAAA==",
"_self": "dbs/QEwcAA==/colls/QEwcALNbIz8=/docs/QEwcALNbIz8GAAAAAAAAAA==/",
"_etag": "\"6a0003c6-0000-0000-0000-5bf7958c0000\"",
"_attachments": "attachments/",
"_ts": 1542952332
}
Please see my test, it could get all the unique categories names.
Sample document:
[
{
"id": "1",
"locales": [
{
"categories": [
"Women",
"clothing",
"tops"
]
}
]
},
{
"id": "2",
"locales": [
{
"categories": [
"Men",
"test",
"tops"
]
}
]
}
]
SQL:
SELECT distinct cat FROM c
join l in c.locales
join cat in l.categories
Output:
[
{
"cat": "Women"
},
{
"cat": "clothing"
},
{
"cat": "tops"
},
{
"cat": "Men"
},
{
"cat": "test"
}
]
If you don't want to case sensitive,just use LOWER function in sql.
SELECT distinct Lower(cat) FROM c
join l in c.locales
join cat in l.categories
If you want to get ["Women","clothing","tops","Men","test"], it can't be parsed as an array in single sql directly, you could use stored procedure to parse the output array.
For example, add below code in stored procedure.
var returnArray = [];
for(var i=0 ;i<array.size;i++){
returnArray.push(array[i].value)
}
return returnArray;

Selenium - json - c#

var pre1 = driver.FindElementByTagName("pre").Text.Replace(#"\", "").Trim();
dynamic root = JsonConvert.DeserializeObject(pre1);
I have this JSON response:
{
"success": true,
"message": null,
"outright": false,
"eventId": 0,
"si": 111,
"leonard": [{
"catalog":[0,0,0,0,0,0],
"edit": 25965112,
"mkilo": {
"888;315;2;3;0": {
"id": 1000,
"description": "Car"
},
"888;316;2;4;0": {
"id": 1001,
"description": "Train"
},
"888;317;2;5;0": {
"id": 1002,
"description": "Airplane"
}
},
"ti": "008000",
"checkin": 254,
"searchCar": {
"id": 1000,
"description": "Car"
}
}],
"ti": 149498
}
verified with jsonlint
root.leonard[0].catalog.Count = 6 ---- > OK
but
root.leonard[0].mkilo.Count = null - -- Why?
I want to read the contents of mkilo.

c# linq-to-sql EF query to match a particular JSON structure

I've JSON with the following structure:
[
{
"ID": 1,
"Label": "Reg Scheme",
"Colours": [
{
"ID": 1,
"Value": "0x3333cc",
"Result": 1,
"Label": null
},
{
"ID": 2,
"Value": "0x666699",
"Result": 2,
"Label": null
},
{
"ID": 3,
"Value": "0x009966",
"Result": 3,
"Label": null
}
]
},
{
"ID": 2,
"Label": "Spesh Scheme",
"Colours": [
{
"ID": 11,
"Value": "0x59699c",
"Result": 1,
"Label": null
},
{
"ID": 12,
"Value": "0x0070ff",
"Result": 2,
"Label": null
},
{
"ID": 13,
"Value": "0x90865e",
"Result": 3,
"Label": null
}
]
},
and I have an entity dataset whereby I've joined all the relevant information, and am attempting to produce JSON with that structure via a single linq-to-sql EF query to be returned to the webapi method.
My query so far is:
return
DbContext.Schemes
.Join(
DbContext.SchemeColours,
s => s.SchemeID,
sc => sc.SchemeID,
(s, sc) => new
{
s.SchemeID,
s.Label,
sc.Colour,
sc.Result,
sc.ColourID
})
.Select(a =>
new Overlay.ReportColourScheme
{
ID = a.SchemeID,
Label = a.Label,
Colours = new List<Overlay.ReportColour>
{
new Overlay.ReportColour
{
ID = a.ColourID,
Value = a.Colour,
Result = a.Result
}
}
})
.ToArray();
Which is almost there but not quite:
[
{
"ID": 1,
"Label": "Regular Scheme",
"Colours": [
{
"ID": 1,
"Value": "0x3333cc",
"Result": 1,
"Label": null
}
]
},
{
"ID": 1,
"Label": "Regular Scheme",
"Colours": [
{
"ID": 2,
"Value": "0x666699",
"Result": 2,
"Label": null
}
]
},
{
"ID": 1,
"Label": "Regular Scheme",
"Colours": [
{
"ID": 3,
"Value": "0x009966",
"Result": 3,
"Label": null
}
]
},
{
"ID": 2,
"Label": "Protanopia adjusted Scheme",
"Colours": [
{
"ID": 11,
"Value": "0x59699c",
"Result": 1,
"Label": null
}
]
},
{
"ID": 2,
"Label": "Protanopia adjusted Scheme",
"Colours": [
{
"ID": 12,
"Value": "0x0070ff",
"Result": 2,
"Label": null
}
]
},
{
"ID": 2,
"Label": "Protanopia adjusted Scheme",
"Colours": [
{
"ID": 13,
"Value": "0x90865e",
"Result": 3,
"Label": null
}
]
},
As of course it creates a new list for every resultID. The top-level ID is a SchemeID- what I'm looking for is logic along the lines of: "take the first 3 Results with a particular schemeID, add them to a list in Colours, then move on to the next schemeID"
I believe this will produce identical JSON that I started the post with.
Any assistance at all would be greatly appreciated, thank you.
Try the following code:
return
DbContext.Schemes
.Join(
DbContext.SchemeColours,
s => s.SchemeID,
sc => sc.SchemeID,
(s, sc) => new
{
s.SchemeID,
s.Label,
sc.Colour,
sc.Result,
sc.ColourID
})
// After joining you group by SchemeID, in this way you have
// for each SchemeID the group of related items
.GroupBy(a => a.SchemeID)
// You then create your result, starting from the main object
.Select(g =>
new Overlay.ReportColourScheme
{
ID = g.Key,
// I suppose you have at least a child for each SchemeID,
// otherwise you can check if the list is empty
Label = g.FirstOrDefault().Label,
// For each group you create a list of child object
Colours = g.Select(v => new Overlay.ReportColour
{
ID = v.ColourID,
Value = v.Colour,
Result = v.Result
}).ToList()
})
.ToArray();
The main issue is that you are using a Join where actually you need a Group Join:
return DbContext.Schemes
.GroupJoin(DbContext.SchemeColours,
s => s.SchemeID,
sc => sc.SchemeID,
(s, colours) => new Overlay.ReportColourScheme
{
ID = s.SchemeID,
Label = s.Label,
Colours = colours
.Select(sc => new Overlay.ReportColour
{
ID = sc.ColourID,
Value = sc.Colour,
Result = sc.Result,
})
.ToList()
})
.ToArray();
But since you are using Entity Framework, it would be much better and eaiser if you define (if you already haven't) and use a navigation property:
class Scheme
{
// ...
public ICollection<SchemeColour> Colours { get; set; }
}
and then simply
return DbContext.Schemes
.Select(s => new Overlay.ReportColourScheme
{
ID = s.SchemeID,
Label = s.Label,
Colours = s.Colours
.Select(sc => new Overlay.ReportColour
{
ID = sc.ColourID,
Value = sc.Colour,
Result = sc.Result,
})
.ToList()
})
.ToArray();

how to JsonConvert.DeserializeObject two data

{
"pagination": {
"next_url": "https://api.instagram.com/v1/users/",
"next_max_id": "98740505131"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"videos": {
"low_bandwidth": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12824263_607003579446709_1011139465_n.mp4",
"width": 640,
"height": 640
},
"low_resolution": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
"width": 480,
"height": 480
}
},
"tags": [],
"type": "video",
"location": null,
"comments": {
"count": 2,
"data": [
{
"created_time": 1457332172,
"text": "什麼東西",
"from": {
"username": "d86241",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11371189_421316874725117_327631552_a.jpg",
"id": 397355082,
"full_name": "Jhao-wei Hvang"
},
"id": 1200511729352353800
},
{
"created_time": 1457771205,
"text": "😀",
"from": {
"username": "realkikog",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11820496_1860868347487361_262727492_a.jpg",
"id": 530665716,
"full_name": "K I K O"
},
"id": 1204194607797938400
}
]
},
"filter": "Crema",
"created_time": 1457326470,
"link": "https://www.instagram.com/p/BCo546hPKpA/",
"likes": {
"count": 22,
"data": [
{
"username": "ladyyihan",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/10684228_590857344404221_1064502415_a.jpg",
"id": 38863087,
"full_name": "Yihan"
},
{
"username": "miding_cyh",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10005160_844941595551352_2014300181_a.jpg",
"id": 226855180,
"full_name": "i😸米丁 Juri"
},
{
"username": "aikoyin1985",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12093346_523536977821971_1279823341_a.jpg",
"id": 2228728531,
"full_name": "aiko1985"
},
{
"username": "sh1recheungg",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/927866_1722116411358109_618748252_a.jpg",
"id": 416683725,
"full_name": "Sh1reCheungg"
}
]
},
"images": {
"low_resolution": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": 1457326470,
"text": "測試東西一下",
"from": {
"username": "jiantai.cai",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
"id": 1417858881,
"full_name": "Mars"
},
"id": 1200463904363030800
},
"user_has_liked": false,
"id": "1200463900470716992_1417858881",
"user": {
"username": "jiantai.cai",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
"id": 1417858881,
"full_name": "Mars"
}
}
]
}
i try
var collection = JsonConvert.DeserializeObject(json);
BUT comments data always get null
like this image
enter image description here
how can i model building this json
i want can
foreach(var item in model){
data.comments.data.text
}
to view
Please help ,
I'm using Newtonsoft 8.0.2 and I was able to get what you are asking with this.
string input = File.ReadAllText("C:\\Public\\input.json");
dynamic collection = JsonConvert.DeserializeObject(input);
foreach (var dataItem in collection.data)
{
dynamic comments = dataItem.comments;
foreach (dynamic comment in comments.data)
{
string text = comment.text;
}
}
What you were missing was that you did not iterate through each comment object under your item.

How to count number of JSON elements in an array in C#

I'm worinkg with instagram API and when I'm receiving recent media with any hashtag by this template:
https://api.instagram.com/v1/tags/{hashtag}/media/recent
I'm receiving data like this:
{
"pagination": {
"next_max_tag_id": "any_number",
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead",
"next_max_id": "any_number",
"next_min_id": "any_number",
"min_tag_id": "any_number",
"next_url": "https://api.instagram.com/v1/tags/{hashtag}/media/recent?access_token={my_personal_access-token}"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"tags": [
"any_tag",
"any_tag1",
"any_tag2",
"any_tag3"
],
"type": "image",
"location": null,
"comments": {
"count": 0,
"data": []
},
"filter": "Normal",
"created_time": "any_number",
"link": "any_url",
"likes": {
"count": 0,
"data": []
},
"images": {
"low_resolution": {
"url": "any_url",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "any_url",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "any_url",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": "any_number",
"text": "any_content",
"from": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
},
"id": "any_number"
},
"user_has_liked": false,
"id": "any_number",
"user": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
}
},
and so on.
As You can see, object "data" is an Array, and further we can see object "tags", which is also array. how can I check number elements array of array in C#? i tried like this:
JArray items = (JArray)jsonData["data[0].tags"];
int length = items.Count;
but it doesn't work. I parse JSON like this:
dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(JSON_string);
var token = JToken.Parse(str);
var data = token.Value<JArray>("data");
var tags = data[0].Value<JArray>("tags");
var count = tags.Count;
You can also use a JsonPath:
var token = JToken.Parse(str);
var count = token.SelectTokens("$.data[0].tags[*]").Count();

Categories

Resources