Reputation: 331
What is the difference between the $match
operator used inside the aggregate function and the regular find
in Mongodb?
Why doesn't the find
function allow renaming the field names like the aggregate function?
e.g. In aggregate we can pass the following string:
{ "$project" : { "OrderNumber" : "$PurchaseOrder.OrderNumber" , "ShipDate" : "$PurchaseOrder.ShipDate"}}
Whereas, find does not allow this.
Why does not the aggregate output return as a DBCursor or a List? and also why can't we get a count of the documents that are returned?
Thank you.
Upvotes: 33
Views: 23623
Reputation: 51
I investigated a few things about the aggregation and find call: I did this with a descending sort in a table of 160k documents and limited my output to a few documents.
ToList()
the aggregation command is faster than the find. Maybe the aggregation automatically calls the ToList()
and does not have to call it again. If you dont call ToList()
afterwards the find()
call will be much faster.
7 [ms] vs 50 [ms] (5 documents)
Upvotes: 0
Reputation: 2397
Why does not the aggregate output return as a DBCursor or a List?
The aggregation framework was created to solve easy problems that otherwise would require map-reduce.
This framework is commonly used to compute data that requires the full db as input and few document as output.
What is the difference between the $match operator used inside the aggregate function and the regular find in Mongodb?
One of differences, like you stated, is the return type. Find operations output return as a DBCursor.
Other differences:
and also why can't we get a count of the documents that are returned?
You can. Just count the number of elements in the resulting array or add the following command to the end of the pipe:
{$group: {_id: null, count: {$sum: 1}}}
Why doesn't the find function allow renaming the field names like the aggregate function?
MongoDB is young and features are still coming. Maybe in a future version we'll be able to do that. Renaming fields is more critical in aggregation than in find.
MongoDB 2.6 aggregation operations will return a cursor.
MongoDB 2.6 was released with the predicted aggregation changes.
Upvotes: 33