MongoDB array sort not working in CosmoDB - mongodb

UPDATE
The CosmoDB team confirmed that there is an issue on their said and they are already working on a fix.
More info in the comment section here: https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb-introduction
ORIGINAL QUESTION
we are planning to migrate to CosmoDB but we found an issue with the $sort command.
In our current MongoDB server running this query:
db.getCollection('Product').find({
"ProductTypeId" : ObjectId("5913546b1ba88338e4347641"),
"SubtypeIngredients" : "5949852c1ba88344d0facbf5"
})
.skip(0).sort({ "IngredientRanks.2.Rank" : 1 }).limit(1)
We get some results but when running the same query in CosmoDB we don't get any results.
if I remove the sort command from the query, I get results from CosmoDB
The data in the collection is the same in our local db and CosmoDB.
Any help would be appreciated.
Thanks!
Update:
Here is an screenshot of the actual query showing the issue.

There is no specific guarantee that CosmoDB supports all the operators and functions of MongoDB, particularly non-trivial use of the API (like chaining sort, skip , etc.). This extends to index optimization and selection as well.

This is what is mentioned in the cosmosdb website

Related

Using Aggregations in RestHeart

I am trying to use the aggregations feature in RestHeart which is described here: https://restheart.org/docs/aggregations/
I am using it to filter and group stuff in my collection based on a input variable, like this.
https://.../_aggrs/test-pipeline?avars={"country":"DE"}
As the documentation states querying the aggregation does not yield the result directly, but I have to query the newly created collection. I found out that it also works to just query the aggregation endpoint twice. But in any case I have to make two requests to get the result.
I am now worried about concurrent users. If two users are querying the aggregation at the same time (with different avars), one might get the result of the other.
I am wondering why this is not mentioned anywhere. It seems to me that everybody should have this problem when using variables (avars) in an aggregation.
How can I solve this? Might transactions be the solution? https://restheart.org/docs/transactions/
I cannot try it right now, because my mongoDB is refusing to start a transaction. But would it even work?
Are there any other solutions?
Best regards,
Tobi

How do I get N random documents from Cosmos DB using the MongoDB API?

I'm using azure cosmos DB acting as MongoDB version 4.0.0. I need to get N random documents from Azure Cosmos DB using the MongoDB API. I've tried using the $sample operator but it is giving me documents in the same order each time I query.
db.collectionName.aggregate([{$sample: {size: 1}}])
No matter how many times I run this query I'm getting the same document from the collection.
I can't find any document on how $sample execute, but I think the problem we met may have relationship with what mentioned here. That means if some conditions are not met, it will get the result and sort them, then return the size count of items. So what we get always the same. I'll try to find more details, and any further find will update.

MongoDB get executionStats for aggregate query

I am looking for a way to retrieve the executionStats for aggregations.
When using find(), I can retrieve them easily by using explain.
Example output:
"executionStats": {
"nReturned": 332505,
"executionTimeMillis": 1349,
"totalKeysExamined": 332505,
"totalDocsExamined": 332505,
...
But when using aggregations with explain enabled it won't return the stats shown above.
This and this is related but there is no viable solution given. Because this might have changed in the meantime, I opened this question.
Is there any way this can be done without measuring the stats on the client side?
Updated Answer:
From MonogoDB version 3.5.5, executionStats and allPlansExecution modes are supported in explain method with aggregation.
db.users.explain("executionStats").aggregate([]);
As per mongodb doc,
You can view more verbose explain output by passing the executionStats or allPlansExecution explain modes to the db.collection.explain() method.
Old Answer:
Currently(MongoDB 3.2) aggregation does not support executionStats, with explain option in aggreagation you get some data related to query but there is no executionStats in it. It is proposed and you can check its status here
https://jira.mongodb.org/browse/SERVER-19758
Please upvote the issue if you want to implement this soon.
For me it works like this, you have this fixed in from version 3.5.5
It shows the execution plan and other metrics.
db.videos.explain('executionStats').aggregate([])
I was able to do it in the following way:
db.myUserCollection.explain("executionStats").aggregate([{$match: {firstName: "John"} }]);
Reference: https://jira.mongodb.org/browse/SERVER-19758
executionStats was not supported for mongo aggregation queries till mongodb v3.4. The issue has been fixed in v3.6.
i.e. db.videos.explain('executionStats').aggregate([])
For mongodb 5.0.0+, just like other answers said, just using db.collections.explain('executionStats').aggregate([]).
If there are NO executionStats, just change your mongodb client.
When I switch DataGrip to mongosh, executionStats appear!

Parse location based query not working after data migration

Parse query withinKilometers() is not working after I migrated the database to my own mongoDB server (No error, but response is empty).
The issue is being discussed in github
But they say it is issue of mongoDB version.
I tried using mongo 3.0.11 , 3.0.9 and 3.0.0
A workaround mentioned is by using cloud code, but the query fails in the cloud too.
Any one have some other workaround please help as the last date of parse data migration is around the corner.
We have to create index for our database column for mongoDB to support geoQuerry, by default it will not support it seems.
Solution:
db.prod.createIndex({ "location": "2d" })
Where prod is my collection name and location is name of column which stores geo location (GeoPoint)
Few more details about the issue is discussed here
Thanks

MongoDB: Is it possible to specify a secondary index and view the execution plan with an update command?

I'm using MongoDB running on Ubuntu.
I'm aware it's not possible to use .hint() or .explain() methods in conjunction with an update command to:
1) specify an Index to be used in the query part of the command, and
2) verify the execution plan
Are there alternatives available to .hint() and 'explain() that can be used with update? I want to update a single record and force Mongo to use the Secondary index rather than the primary.
Grateful for any suggestions,
Jon
As noted in one of my comments to the question, as of MongoDB version 2.6.3 there is no alternative to $hint for an update. However, it does seem to be planned for future releases, as there is an open JIRA ticket for that feature with the status "planned but not scheduled".
It is a similar story for $explain. However, it does appear that this feature will be coming soon. There is a JIRA task for revamping explain() and one of its subtasks is entitled "Explain Command for Explaining Update and findAndModify". The value for the "Fix Version" field is "2.7 Required".