search by substring in documentDB - c#

this is the sample documentDB document,
I want to get all the documents who failed in one or more subjects
I found something like
SELECT
*
FROM students s
JOIN c IN s.subjects
WHERE c.result = "pass"
I want to retrieve by using c# code
{
"id": "0066a253-f042-4213-b06e-65b1ea1e49aa",
"name": "Sunny",
"rollNo": 123,
"class": "2nd",
"section": "B",
"Department": {
"name": "CSE",
"id": "cse",
"subjects": [
{
"id": "subject-1",
"marksObtained": 66,
"maxMarks": 100,
"result": "pass"
},
{
"id": "subject-2",
"marksObtained": 56,
"maxMarks": 75,
"result": "pass"
},
{
"id": "subject-3",
"marksObtained": 22,
"maxMarks": 100,
"result": "fail"
},
{
"id": "subject-4",
"marksObtained": 36,
"maxMarks": 50,
"result": "pass"
},
{
"id": "subject-5",
"marksObtained": 16,
"maxMarks": 100,
"result": "fail"
}
]
},
"Type": "Student"
}
i tried like this
var result = client.CreateDocumentQuery<dynamic>(dc.SelfLink, "SELECT s.id as id,s.Name as Name,s.Age as Age,s.section as section,s.subjects as subjects FROM students s JOIN c IN s.subjects WHERE c.result = \"pass\"").ToList();
List<Student> students = new List<Student>();
foreach(var std in result)
{
students.Add((Student)std);
}
Something like above is my code I am getting, but Even I give pa or pas or pass or p or ass or as then also I should get something I need a functionality of LIKE in SQL
Is there any solution for this??
I need LIKE functionality in SQL to retrieve data from documentDB

Update: As of 5/6/15, DocumentDB added a set of String functions including STARTSWITH, ENDSWITH, and CONTAINS. Please note that most of these functions do not run on the index and will force a scan.
Wildcards like SQL's LIKE '% %' has not been implemented in DocumentDB yet.
Please voice your opinion and vote for this feature on DocumentDB's feedback forum.

Some new functions have been introduced in the last few months. For you particular case I think you can use:
WHERE STARTSWITH(c.result, "p")

Related

Querying CosmosDB Child Collection using ORDER BY

Say I have a document like below for each userId. And each userId has a child collection object named "lnk" and it can grow up to 100 items per userId.
I would like to sort the child collection for a given userId based on a single property
(ex: Topic or Pid or URL). for userId = "5663a8f7-6d2e-40ef-8972-515944080474"
SELECT * FROM c IN PageLinksContainer.lnk order by c.top asc OFFSET 1 LIMIT 3
Error I'm getting:
Failed to query item for container PageLinksContainer:
{
"errors": [
{
"severity": "Error",
"location": {
"start": 7,
"end": 25
},
"code": "SC2001",
"message": "Identifier 'PageLinksContainer' could not be resolved."
},
{
"severity": "Error",
"location": {
"start": 67,
"end": 85
},
"code": "SC2001",
"message": "Identifier 'PageLinksContainer' could not be resolved."
}
]
}
{
"userid": "5663a8f7-6d2e-40ef-8972-515944080474",
"lnk": [
{
"pid": 1,
"top": "Topic 1",
"por": "www.google.com",
"sdt": "10/26/2021"
},
{
"pid": 2,
"top": "Topic 2",
"por": "www.google.com",
"sdt": "10/26/2021"
},
{
"pid": 3,
"top": "Topic 3",
"por": "www.google.com",
"sdt": "10/26/2021"
}
]
}
C#
var query = "SELECT * FROM c IN PageLinksContainer.lnk OFFSET 1 LIMIT 10"; //Works fine with no order by.
var container = _cosmosClient.GetContainer(_databaseName, containerName);
using var iterator = container.GetItemQueryStreamIterator(new QueryDefinition(query),
requestOptions: new QueryRequestOptions
{
PartitionKey = new PartitionKey("5663a8f7-6d2e-40ef-8972-515944080474"),
MaxItemCount = 100
});
Links:
https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-order-by
CosmosDB sql query with/without "ORDER BY" returns different number of items
You can't ORDER BY properties in arrays currently.
The following query better demonstrates that with the following error Order-by over correlated collections is not supported:
SELECT VALUE l
FROM c
JOIN l IN c.lnk
ORDER BY l.por
What you can do is create a User Defined Function that sorts the array before return. Although I would advise you to retrieve the results and use OrderBy in your C# code as it's easier, more transparant, and avoids spending unnecessary RU's on an UDF.

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 can I push an array into aggregate pipeline and pull it one level up?

I am new in MongoDB and I am developing a software by C# and MongoDB. My data structure is like this
{
"Id": 1,
"Title": "myTitle",
"Geners": [ "Drama", "Action" ],
"Category": 1,
"Casts": [
{
"Id": 1,
"Name": "myName",
"Gender": "Male",
"Age": 35
},
{
"Id": 2,
"Name": "herName",
"Gender": "Female",
"Age": 30
},
{
"Id": 3,
"Name": "hisName",
"Gender": "Male",
"Age": 45
}
]
}
This is just one document and I have about 5 million documents. I want to run a query like below to count the records based on Category and shows me how many movie do I have in each category and I want to put Casts field in result.
db.getCollection('myCollection').aggregate([
{
$group:{"_id":"$Category", "count": {$sum:1},
"Casts":{$push:"$Casts"}}
}
])
this is close to something I want but the problem is, it puts Casts data in second level of array like {"Id":1, ... , "Casts":[[{},{},...]]} but I need it like this {"Id":1, ... , "Casts":[{},{},...]}
How can I show the data like that?
If duplicates are acceptable, then the following aggregation will suffice:
db.getCollection('myCollection').aggregate([
{ $unwind:"$Casts"},
{
$group:{"_id":"$Category", "count": {$sum:1},
"Casts":{$push:"$Casts"}}
}
])
Update:
Since you need the count to be valid, there's a few more hoops to jump through.
db.getCollection('myCollection').aggregate([
{ $group:{"_id":"$Category", "count": {$sum:1}, "Casts":{$addToSet:"$Casts"}}},
{$unwind:"$Casts"},
{$unwind:"$Casts"},
{ $group:{"_id":"$_id", "count": {$first:"$count"}, "Casts":{$addToSet:"$Casts"}}},
])
Let me know if that helps

Getting whole documents from MongoDB and C# by distinct element

I'm having a hard time understanding how I should write some MongoDB queries.
Maybe my mind is too accustomed to relational databases.
Anyway, I want to retrieve all documents (whole documents, not a subset of elements) but only one per distinct value of a element.
For example, I have the following 3 documents in a collection:
{
"person": {
"name": "james",
"age": "21",
"city": "London"
}
},
{
"person": {
"name": "edith",
"age": "18",
"city": "London"
}
},
{
"person": {
"name": "steve",
"age": "29",
"city": "Berlin"
}
}
I want to retrieve whole documents but only with distinct "city" element values. The rest of the data should be there (hence why I cant just $group them) it just doesn't matter which document among the subset that gets returned.
So the desired output should be (in case we always use the first document with the distinct value): (The first document could just aswell be edith, doesn't matter)
{
"person": {
"name": "james",
"age": "21",
"city": "London"
}
},
{
"person": {
"name": "steve",
"age": "29",
"city": "Berlin"
}
}
Did that make sense?
(Dummy data, but the problem is a real one)
I believe what your are looking for is:
GetCollection<Person>("person").DistinctAsync("person.city",filter)

Categories

Resources