What will be the LINQ equivalent of this cosmos SQL API query:
SELECT c as Person, ST_DISTANCE(c.location, {'type': 'Point', 'coordinates':[-122.3382419, 47.6074856]}) as Distance
FROM c
WHERE c.DocType = 0 AND
ST_DISTANCE(c.location, {'type': 'Point', 'coordinates':[-123.3382419, 47.6074856]}) < 1 AND NOT c.Blocked
which gets back
[
{
"Person": {
"DocType": 0,
"location": {
"type": "Point",
"coordinates": [
-123.3382419,
47.6074856
]
},
"MDS": "Chwal",
"Description": "Bduwhs",
"Contents": null,
"GFree": false,
"Veg": false,
"AllergicContents": null,
"Calorie": 0,
"LinkToRecipe": null,
"Cost": 36,
"DASD": "Mexican",
"ExpirationTime": "2019-12-17T11:30:52Z",
"Images": [
"test/25254932-2898-5fd7-949b-b2feb25a4964"
],
"ProducerUserId": "1b36c0f1-425c-483a-bb01-69b06e69f203",
"ExchangeDateTimeStartInUtc": "2019-12-17T20:30:52Z",
"ExchangeDateTimeEndInUtc": "2019-12-17T19:30:52Z",
"Blocked": false,
"id": "asd,
"_rid": "asd=="
},
"Distance": 0.1
}
]
The problem with ST_DISTANCE is that it can only be calculated on Cosmos db, and can't be post evaluated.
Found a way to do it other than writing SQL query string
DocumentDBRepository.RunQueryAsync(
from c in DocumentDBRepository.DocumentQuery<Person>()
where c.DocType == 0
&& c.Location.Distance(point) < 1
&& !c.Blocked
select new { Person = c, Distance = c.Location.Distance(point) });
Also realized that LINQ lambda is different from a LINQ query. The above is a LINQ query, A LINQ lambda looks like this
Related
Suppose we have a structure:
{
"nested_items": [
{
"nested_sample0": "1",
"nested_sample1": "test",
"nested_sample2": "test",
"nested_sample3": {
"type": "type"
},
"nested_sample": null
},
{
"nested_sample0": "1",
"nested_sample1": "test",
"nested_sample2": "test",
"nested_sample3": {
"type": "type"
},
"nested_sample1": null
},
...
],
"sample1": 1233,
"id": "ed68ca34-6b59-4687-a557-bdefc9ec2f4b",
"sample2": "",
"sample3": "test",
"sample4": "test",
"_ts": 1656503348
}
I want to retrieve documents by id by with limit of "nested_items" field .As I know limit and offset not supported in sub queries. Any way to do this except of divide into two queries? Maybe some udf or else?
You can use the function ARRAY_SLICE assuming the array is ordered.
Example data:
{
"name": "John",
"details": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
Example queries
-- First 2 items from nested array
SELECT c.name, ARRAY_SLICE(c.details, 0, 2) as details
FROM c
-- Last 2 items from nested array
SELECT c.name, ARRAY_SLICE(c.details, ARRAY_LENGTH(c.details) - 2, 2) as details
FROM c
{
"Name": "Naveen",
"Age": 28,
"IS_MEMBER": false,
"Products":[
{
"id": 1,
"NAME": "XXXX"
"COST": 5.99
},
{
"id": 2,
"NAME": "XXXX"
"COST": 4.99
}
]
}
What you need is,
SELECT SUM(a.COST) AS SumTotal
FROM a in c.Products
This can be achieved using "Join".
SELECT sum(t.Cost) as SumTotal FROM c join t in c.Products
I am using Azure Cosmos DB and trying to write a query to filter document by Name and version. I am new to Cosmos and it seems the way I'm doing applies the filter per record versus the results themselves. Can anyone tell me the proper way to accomplish this:
select C.*
from c
JOIN (select MAX(c.version) from c where c.name = "test") maxVersion
where maxVersion = c.version
Sample data:
[{"name":"test","verson":1}{"name":"test","verson":2}{"name":"test","verson":3}]
Results:
I get a record back for each version vs the max version. IE I only should get one record back and it's version number should be 3
When you run this SQL:
select c,maxVersion
from c
JOIN (select MAX(c.version) from c where c.name = "test") maxVersion
you will get this document:
{
"c": {
"id": "1",
"name": "test",
"version": 1
},
"maxVersion": {
"$1": 1
}
}
{
"c": {
"id": "2",
"name": "test",
"version": 2
},
"maxVersion": {
"$1": 2
}
},
{
"c": {
"id": "3",
"name": "test",
"version": 3
},
"maxVersion": {
"$1": 3
}
}
Your maxVerson equals to c.version in each document, so you will get multiple documents not one.
According to your requirement, you can try something like this SQL:
SELECT TOP 1 *
FROM c
WHERE c.name = "test"
ORDER BY c.version DESC
I am trying following code to get details but it is not return what i want .
my query :
var query = $"SELECT catalog.id as vendorguid,catalog.VendorName,industry.Id as industryguid,industry.IdustryId,industry.IdustryName,category.id as categoryguid,category.Name as categoryname,category.Description as categorydescription,Subcategory.Id as subcategoryguid,Subcategory.Name as subcategoryname,Subcategory.Description as subcategorydescription,product.Id as productguid,product.Name as productname,product.CurrentQuantity as productCurrentQuantity,product.Tag as productTag,product.ImageUrl as productImageUrl,product.Unit as productUnit,product.Price as productPrice,product.hasMethodOfPreparation as producthasMethodOfPreparation,product.MethodOfPreparation as productMethodOfPreparation,product.Addons as productAddons FROM catalogjoin industry in catalog.Industy join category in industry.Category join Subcategory in category.Subcategoryjoin product in Subcategory.Product where product.Id = '"+productId+"'";
my service class :
output:
{
"id": "d91af3e6-6aae-4d10-9ba6-6b97ca4cd881",
"vendorName": "string",
"industy": []
}
expected output:
{
"vendorguid": "97392a23-c8c4-4a7f-bffb-7c3807cc40de",
"VendorName": "string",
"industryguid": "f8265ee6-a351-4036-a0ee-10ec4f51ecf4",
"IdustryId": 0,
"IdustryName": "string",
"categoryguid": "ce7edfdf-1608-4a8c-ae38-7ca60c43bc30",
"categoryname": "string",
"categorydescription": "string",
"subcategoryguid": "709f4e6f-1bba-421f-8dc0-7cf067cbc032",
"subcategoryname": "string",
"subcategorydescription": "string",
"productguid": "d24dd340-d7a2-42e5-b84a-1ee72d9840c6",
"productname": "string",
"productCurrentQuantity": 0,
"productTag": "string",
"productImageUrl": [
"string"
],
"productUnit": 0,
"productPrice": 0,
"producthasMethodOfPreparation": true,
"productMethodOfPreparation": [
],
"productAddons": [
]
}
how can i get it anyone help me?
I think the issue is in your joins (Category, SubCategory). Are your sure its always finding a match? If it doesn't find a match or don't have that properties, CosmosDb will not include it in the results set.
Its not like the left join of MSSQL where nulls are included. CosmosDb is more of a full outer join (cross join).
I have below structure
{
"SubscriberId": "a4db02c1-f41b-4ab1-9f3e-83f9a7ccde83",
"Subscription": {
"Type": "Member",
"MaxUsers": "200",
"StartDate": "2018-07-01T00:00:00.0000000Z",
"EndDate": "2019-07-31T00:00:00.0000000Z",
"Families": [
{
"Id": "4042e5dc-ff0e-5cca-d5ee-d96c4522f1db",
"Products": [
{
"Id": "e9313211-ca18-4776-8fea-1d552b6f40d6",
"Price": null
},
{
"Id": "bf3cdaa4-8cbe-42e6-8f6f-20c1210dc4ac",
"Price": null
}
]
}
],
"IsActive": "true"
}
}
so I want to get data in the below structure, how can I use sql to do this?
SubscriberID , Type,Families.Id,Families.Products.Id, Families.Products.Price
Please use sql:
select distinct c.SubscriberId,c.Subscription.Type as Type,
f.Id as FamiliesId,p.Id as ProductsId,p.Price as ProductsPrice
from c
join f in c.Subscription.Families
join p in f.Products
Result: