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.
Related
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. =]
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
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
I'm currently working on a project where we are using couchbase 4.1 as of today for a eCommerce site.
I want to store our websites entire category structure in Couchbase as a single document and then query for a specific category and return that category in some cases and in other cases I would like to return the category and its child categories.
I'm pretty sure I have to use the array indexeer to make this work efficient but I'm quite new to Couchbase so I'm not sure how it should be structured (or even if it's possible).
Part of my document looks like this (there is 4 levels in the structure and about 8-10 top level categories):
{
"Categories": [
{
"DisplayName": "Category One",
"Id": 1,
"Categories": [
{
"DisplayName": "Child category",
"Id": 10,
"Categories": [
{
"DisplayName": "Child child category",
"Id": 100,
"Categories": [
{
"DisplayName": "Child child child category",
"Id": 1000
},
{
"DisplayName": "Sibling child category",
"Id": 1001
}
]
},
{
"DisplayName": "Child",
"Id": 101,
"Categories": [
{
"DisplayName": "Another child category",
"Id": 2001
}
]
}
]
}
]
}
]
}
If I query for Id = 100 I would like to have my result look like this:
{
"DisplayName": "Child child category",
"Id": 100,
"Categories": [
{
"DisplayName": "Child child child category",
"Id": 1000
},
{
"DisplayName": "Sibling child category",
"Id": 1001
}
]
}
In some cases I am not interessted having the childs. I have tried to create my query using the array (N1QL) to select into my arrays but I'm not sure whether it's even possible when having levels of complex objects.
Can give me some guidedance on how this is possible (even if it is?). We are using the Couchbase .NET client.
Best regards Martin
This is interesting, because you are trying to store everything inside one document, and then query into that document. Here is one approach.
To query Category Id 100 without sub-categories:
SELECT c.Id, c.DisplayName
FROM default
UNNEST ( ARRAY cat FOR cat WITHIN Categories WHEN cat.Id IS NOT NULL END ) AS c
WHERE c.Id = 100;
To query Category Id 100 with sub-categories:
SELECT c.Id, c.DisplayName, c.Categories
FROM default
UNNEST ( ARRAY cat FOR cat WITHIN Categories WHEN cat.Id IS NOT NULL END ) AS c
WHERE c.Id = 100;
To query Category Id 100 with only one level of sub-categories:
SELECT c.Id, c.DisplayName, sub.Id AS SubId, sub.DisplayName AS SubDisplayName
FROM default
UNNEST ( ARRAY cat FOR cat WITHIN Categories WHEN cat.Id IS NOT NULL END ) AS c
LEFT OUTER UNNEST c.Categories AS sub
WHERE c.Id = 100;
The only thing i can find is subdoc (first available in Couchbase 4.5): http://blog.couchbase.com/2016/february/subdoc-explained
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")