MongoDB get executionStats for aggregate query - mongodb

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!

Related

Does mongodb use index search in lookup stage?

I'm querying a collection with aggregate function in MongoDB and I have to look up some other collections in its aggregation. But I have a question about it:
Does MongoDB use indexes for foreignField? I wasn't able to figure this out and I searched
everywhere for this but I didn't get my answer. It must certainly use indexes for it but I just want to be sure.
The best way to determine how the database is executing a query is to generate and examine the explain output for the operation. With aggregations that include the $lookup stage specifically you will want to use the more verbose .explain("executionStats") mode. You may also utilize the $indexStats operator to confirm that the usage count of the intended index is increasing.
The best answer we can give based on the limited information in the question is: MongoDB will probably use the index. Query execution behavior, including index usage, depends on the situation and the version. If you provide more information in your question, then we can provide more specific information. There is also some details about index usage on the $lookup documentation page.

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 to do the performance evaluation of findOneAndUpdate() after indexing in mongo

I could see .explain("executionStats") to evaluate the performance of find() queries in Mongo. But .explain is not working on findOneAndUpdate(). Is there a way to see how much the indexing is helping findOneAndUpdate() queries
in explain("executionStats"). you just remove "executionStas" and run your findOneAndUpdate query just as below
'''
db.collection.explain().findOneAndUpdate({query})
'''
it will working fine...
Unfortunately you cannot use .explain() and .findOneAndUpdate() together. .explain() can be used with those functions that return cursor id, like .find() but it cannot be used with .findOne(), .findOneAndUpdate() and similar functions because they return a single result which is not a cursor id.
What I would suggest is to have a look at these two blog posts by Justin Liu of Rockset.
Handling Slow Queries in MongoDB - Part 1 - Investigation
Handling Slow Queries in MongoDB - Part 2 - Solutions

Aggregation Execution Stats (Mongo)

I can see in the documentation that explain("executionStats") is available for some of the mongo queries which is really helpful but I can't see such utility for the aggregation query, apart from the explain flag which doesn't give much. Is there a plan to integrate the full overview in the aggregation query too?
This function can be applied to aggregate queries, it just needs to be called before the .aggregate() function, and with 'executionStats', otherwise you will only get details on the query's pipeline and not it's execution.
db.collection.explain('executionStats').aggregate([..]);

MongoDB array sort not working in CosmoDB

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