DocumentDB query nested object - c#

I'm having trouble querying nested objects in DocumentDB. I have no control over the format of the data. Let's say an object looks like this in DocumentDB:
{
"SCHEMA_ID": {
"PROJECT": "A",
"MODEL": "B",
"GUID":"A GUID"
},
"STATE": {
"Active": "True"
},
"OBJECTS": {
"OBJECT": [
{
"ATTR_VALS": {
"NAME": "Header",
"ID": "0",
"VALUE": [
{
"NAME": "JobId",
"VAL": "1011656"
},
{
"NAM": "Region",
"VAL": "West Coast"
}
]
}
},
{
"ATTR_VALS": {
"NAME": "SampleData",
"ID": "0",
"VALUE": [
{
"NAME": "Height",
"VAL": "5"
},
{
"NAM": "Length",
"VAL": "3"
}
]
}
}
]
}
}
I want to find all the objects that have a 'ATTR_VALS' = 'SampleData' and where those items have a 'Height'=5
So Far I have:
SELECT test.GUID
FROM test
join OBJECTS in test.OBJECTS
join OBJECT in OBJECTS
join ATTR_VALS in OBJECT
join VALUE in ATTR_VALS
WHERE ATTR_VALS.NAME = 'SampleData' AND VALUE.NAME='Height' AND VALUE.VAL='5'
But this doesn't work, and returns no results. Thanks!

The query must be:
SELECT test.SCHEMA_ID.GUID
FROM test
join OBJ in test.OBJECTS.OBJECT
join VAL in OBJ.ATTR_VALS["VALUE"]
WHERE OBJ.ATTR_VALS.NAME = "SampleData" AND VAL.NAME='Height' AND VAL.VAL='5'
A couple things I changed:
JOIN must be performed against arrays, not objects. Objects can be expanded using the “.” Operator
VALUE is a special keyword and must be escaped
Small typo in the projection clause missing SCHEMA_ID

Related

Mongodb Lookup with sorting and grouping in C#

I have the following db config:
db={
"order": [
{
"id": 1,
"version": 1
},
{
"id": 1,
"version": 2
},
{
"id": 2,
"version": 1
},
{
"id": 2,
"version": 2
}
],
"orderDetail": [
{
"orderId": 1,
"orderDate": new Date("2020-01-18T16:00:00Z")
},
{
"orderId": 1,
"orderDate": new Date("2020-01-11T16:00:00Z")
},
{
"orderId": 1,
"orderDate": new Date("2020-01-12T16:00:00Z")
}
]
}
I'm using the fluent interface to perform a Lookup joining the orderDetails to the order collection (as shown in this post). Now that I have the join in place what's the best method to:
Sort the joined array such that the details are sorted by orderDate
Group the Orders (by OrderID) and sort by version to select the latest (largest Version #)
The workaround I implemented for #1 involves sorting the list after performing the lookup, but that's only because I wasn't able to apply a sort to the "as" of collection as part of the Lookup.
If anyone has any ideas, I'd appreciate it. Thanks!
If you are using MongoDB v3.6 or higher, you can use the $lookup with uncorrelated subqueries to use the inner pipelines to archive what you want.
Join Conditions and Uncorrelated Sub-queries
Since you didn't provide what collections or fields you are using, I will give a generic example:
db.customers.aggregate([
{
$lookup: {
from: "orders",
let: { customer_id: "$_id" },
pipeline: [
{ $match: { $expr: { $eq: [ "$customer_id", "$$customer_id" ] } } },
{ $sort: { orderDate: -1 } }
],
as: "orders"
}
}
]);
I hope that gives you a way to get where you want. =]

How to make this CosmosDB SQL Query work without knowing ARRAY index?

I am querying a CosmosDB in such a way that I am getting a string in and ned to return some data out through a C# WEB API, the query that works for me is as below
SELECT *
FROM c IN jongel.OriginalData.base.sales.variants
WHERE c.globalTradeItemNumber.globalTradeItemNumberType[0].GTIN = '1111111111111'
The problem is that I have to know the ARRAY INDEX for the globalTradeItemNumberType ARRAY, [0] in this example, for it to work but it is not always 0, it could be any number from 0-9 basically and I cannot figure out how to rewrite the query so that it works regardless of the index where the matching data is found?
How can I rewrite this query so that I do not need to know the ARRAY INDEX beforehand?
--- EDIT ---
A sample document shortened to only include the needed parts
{
"id": "635af816-8db7-49c6-8284-ab85116b499b",
"brand": "XXX",
"IntegrationSource": "XXX",
"DocumentType": "Item",
"ItemInformationType": "",
"ItemLevel": "Article",
"ItemNo": "0562788040",
"UpdatedDate": "1/1/2020 4:00:01 AM",
"UpdatedDateUtc": "2020-01-01T04:00:01.82Z",
"UpdatedBy": "XXX",
"OriginalData": {
"corporateBrandId": "2",
"productId": "0562788",
"articleId": "0562788040",
"season": "201910",
"base": {
"sales": {
"SAPArticleNumber": "562788040190",
"simpleColour": {
"simpleColourId": "99",
"simpleColourDescription": "Green",
"translatedColourDescription": [
{
"languageCode": "sr",
"simpleColourDescription": "Zeleno"
},
{
"languageCode": "zh-Hans",
"simpleColourDescription": "绿色"
},
{
"languageCode": "vi-VN",
"simpleColourDescription": "Xanh la cay"
}
]
},
"variants": [
{
"variantId": "0562788040001",
"variantNumber": "562788040190001",
"variantDescription": "YYYYYYYYY, XXS",
"sizeScaleAndCode": "176-001",
"netWeight": 0.491,
"unitsOfMeasure": {
"unitsOfMeasureType": [
{
"alternativeUOM_ISO": "PCE",
"length": 320,
"width": 290,
"height": 31,
"unitOfDimension": "MM",
"volume": 2876.8,
"volumeUnit": "CCM",
"weightUnit": "KG"
}
]
},
"globalTradeItemNumber": {
"globalTradeItemNumberType": [
{
"GTIN": "1111111111111",
"GTINCategory": "Z3"
},
{
"GTIN": "2222222222222",
"GTINCategory": "Z3"
},
{
"GTIN": "3333333333333",
"GTINCategory": "IE"
}
]
}
}
]
}
}
}
}
I tried the following query based on suggested answer below but it did not work
SELECT *
FROM c
WHERE ARRAY_CONTAINS(c.OriginalData.base.sales.variants.globalTradeItemNumber.globalTradeItemNumberType, {GTIN:"1111111111111"}, true)
I guess the above fails because variants part of the tree is also an array?
NOTE: the variants array can hold several objects so its not always index[0]
You could try using the ARRAY_CONTAINS function.
SELECT *
FROM c IN jongel.OriginalData.base.sales.variants
WHERE ARRAY_CONTAINS(c.globalTradeItemNumber.globalTradeItemNumberType, {GTIN:"1111111111111"}, true)
This will allow the query to search all items in the array for a matching GTIN value.
https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-array-contains

How to ascii sort a List of User Defined Collection in C#

I have a list of collection (objects) where I need to ascii sort them by a specific key (value)
I have tried but all I can find anywhere is a list of strings being ascii sorted instead of objects.
var myList = new List();
And this mylist is as below:
[
{
"parent": null,
"Id": 14108,
"value": ""
},
{
"parent": null,
"Id": 14109,
"value": null
},
{
"parent": null,
"Id": 14113,
"value": "1ab"
},
{
"parent": null,
"Id": 14114,
"value": "11b"
},
{
"parent": null,
"Id": 14115,
"value": "a10"
},
{
"parent": null,
"Id": 14111,
"value": "a1234"
}
]
I expect the list to be sorted by the value in ascii order but seems a problem only solved by list of strings in c#
You need to pass an IComparer to your sort method or use a lambda to identify which property should be used to sort. In your example it looks something like myList.Sort( f => f.value )
given
List<MyInterfaceObject> mylist;
then
mylist.Sort(x => x.value);

Query Azure DocumentDB that has two attributes with the same name

I'm trying to query DocumentDB by the following attribute
"Loop": [
{
"#LoopId": "0204",
"#Name": "Order Information Detail",
"OID": {
"OID01": null,
"OID02": "1NN***"
}
},
{
"#LoopId": "0210",
"#Name": "Quantity Information",
"QTY": {
"#comment": [],
"QTY01": {
"#text": "H9"
},
"QTY02": "10",
"QTY03": null,
"QTY04": "Pallets"
}]
using the Query
SELECT * FROM c WHERE c.Loop[0].OID.OID02="1NN***";
however the same document has another attribute Loop in it:
"Loop": [
{
"#LoopId": "0100",
"#Name": "Party Identification",
"N1": {
"#comment": [],
"N101": {
"#text": "CA"
},
"N102": "BN*********",
"N103": {
"#text": "2"
},
"N104": "B***"
}
},
{
"#LoopId": "0100",
"#Name": "Party Identification",
"N1": {
"#comment": [],
"N101": {
"#text": "SF"
},
"N102": "*****"
}]
is there anyway around this because I need to get the information by OID
thanks in advance
Can you try doing this query, as I think each "row" in the Loop array is a different Loop object:
SELECT * FROM Loop c WHERE c.OID.OID02 = "1NN***";

how to make json object in c#?

I have all values from aspx page. now I want to prepare following json values by picking up from the control values of aspx where user has inputted and want to submit to other application in same format.For that below is the example I want to make exact copy as like below.
var tempk = {
"requestTypeCode": "PRE_DETERMINATION",
"billingProvider": {
"npi": "1234567893",
"ein": "111222333",
"payerAssignedProviderId": "XYZ321"
},
"patient": {
"relationshipCode": "01",
"lastName": "Smith",
"firstName": "Bob",
"stateCode": "FL",
"birthDate": "1980-02-12",
"genderCode": "M"
},
"payer": {
"id": "BCBSF"
},
"submitter": {
"id": "123456789",
"lastName": "SUBMITTER"
},
"subscriber": {
"memberId": "JDH001",
"groupName": "ASDF 1-2",
"groupNumber": "12312412"
},
"claimInformation": {
"placeOfServiceCode": "11",
"diagnoses": [
{
"qualifierCode": "ABK",
"code": "J3089"
}
],
"serviceLines": [
{
"procedureCode": "92523",
"quantity": "100",
"amount": "250",
"fromDate": "2016-05-10"
}
]
}
}
Can you assist me how with the C# code will achieve?
You can use Newtonsoft.Json from nuget
http://www.newtonsoft.com/json

Categories

Resources