I can’t find anywhere in the MongoDB docs which explains whether basic queries are the better choice over aggregation pipelines for a straightforward scenario where I’m not doing any groupings or transformations.
For example, suppose I have a large collection “Books” (5million docs) with the following index: { published: 1, title: 1, date: 1}.
Would it be better/faster for me to use this basic query:
db.Books.find(
{ "published": true, "metadata.xyz": "testValue" },
{ "title": 1, "author": 1},
{ "sort": { "title": 1 }, "skip": 5, "limit": 10 }
);
or this aggregation pipeline:
db.Books.aggregate([
{ $match: {"published": true, "metadata.xyz": "testValue"} },
{ $project: {"title": 1, "author": 1} },
{ $sort: { "title": 1 } },
{ $skip: 5 },
{ $limit: 10 },
]);
to get my results? (I am not able to index the metadata field due to its wide variation of values)
If possible, please share a reference to documentation that explains why one is better/worse than the other for the above situation (or if you’re an official MongoDB employee, that works too)