question

LadislavBhm-9362 avatar image
0 Votes"
LadislavBhm-9362 asked OuryBa-MSFT commented

CosmosDB with Mongo API throws 500

Hi,

I'm using CosmosDB with Mongo API version 4 and C# mongo driver. I have a query that I believe should work (it works in MongoDB) but throws 500 error in one specific case. Issue happens when I try to upsert a document that does not yet exist and within the update I push items to an array and include a sort operator (like mentioned here in MongoDB documentation. If the document already exists and then the query works in both CosmosDB and MongoDB. If the document not yet exists and is being upserted then it fails in CosmosDB and works in MongoDB. Query looks like this:

var update = Builders<UserLocationBucket>.Update
.Set(bucket => bucket.ETag, ObjectId.GenerateNewId().ToString())
.PushEach(bucket => bucket.Locations, locations, sort: Builders<Location>.Sort.Ascending(l => l.Timestamp))
.SetOnInsert(bucket => bucket.Id, ObjectId.GenerateNewId())
.SetOnInsert(bucket => bucket.StartDate, startDate)
.SetOnInsert(bucket => bucket.EndDate, endDate);

await m_UserLocations.UpdateOneAsync(filter, update, new UpdateOptions {IsUpsert = true});

Notice on 3rd line "sort: Builders<Location>.Sort.Ascending(l => l.Timestamp)". If I remove this operator from the query it works in all cases in both CosmosDB and MongoDB. I believe this is might be a bug in CosmosDB engine because I could not find any mention of this being unsupported in documentation.








azure-cosmos-db
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @LadislavBhm-9362 Thank you for posting your question. We are working internally with our PG to reproduce this issue.
Regards,
Oury

0 Votes 0 ·

Hi, if you need more info like raw mongodb query that goes to server let me know.

0 Votes 0 ·

Hi @LadislavBhm-9362 Could you please check if you are missing the compound index that is required to sort results: Manage indexing in Azure Cosmos DB's API for MongoDB | Microsoft Docs.
Regards,
Oury


0 Votes 0 ·
Show more comments

0 Answers