Closed
Description
Currently Mongoose supports query builder, explained here https://mongoosejs.com/docs/queries.html#executing
// Using query builder
Person.
find({ occupation: /host/ }).
where('name.last').equals('Ghost').
where('age').gt(17).lt(66).
where('likes').in(['vaporizing', 'talking']).
limit(10).
sort('-occupation').
select('name occupation').
exec(callback);
One can however also use the Aggregation pipelines to achieve similar results (this example is not a 1-1 with the one above, but it should be feasible).
Model.
aggregate([{ $match: { age: { $gte: 21 }}}]).
unwind('tags').
exec(callback);
I could not find anywhere in the documentation as why you would want to use one over the other. Aggregation is a mongodb concept, and should be the most efficient I would recon, but this depends on the implementation details in mongoose for queries.
Could the documents be updated to shed some light on this, or am I missing something fundamental/obvious in the differences?
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
[-]Question: Aggregate vs. Query buildr[/-][+]Question: Aggregate vs. Query builder[/+]AbdelrahmanHafez commentedon Jan 12, 2020
The way I see it, the aggregation framework is MongoDB on steroids, but it comes with a hefty price tag.
Generally, I only use the aggregation framework ONLY when I have to, mostly when I have to
$group
documents by one or more fields, and do some calculations on some of the fields. It gives you a lot that you can express about, the downside of it is maintainability. You'll often find yourself shuffling through stages in order to understand what's going from one stage into the other, and making a change takes a lot of time.Queries, however, cover most of the cases you need if you know enough MongoDB, and they are more maintainable IMO, it's easier to look at a query and know how the result exactly looks like, and why it looks that way.
As for performance, @vkarpov15 explains in those two articles cases where the aggregation framework could actually have less performance.
Personally, I don't find performance here as a serious issue, since most applications development is bottlenecked by maintainability rather than performance. I would choose good old queries over an aggregation pipeline any day. Sometimes you don't have that luxury, though.
As for the docs, I don't know if such thing belongs to mongoose or MongoDB docs. I'll have to wait for @vkarpov15 to let me know what he thinks. Should we add it to our docs? I may write an article with what I know about both, and when we should choose one over the other.
thernstig commentedon Jan 12, 2020
@AbdelrahmanHafez thank you for the good input. I think that was one of the answers I was after. It is not clear to me if I should use the query builder or aggregation pipeline. And since the query builder is kind of mongoose specific, I would have expected to have some docs, maybe just a short section/paragraph, explaining how to reason about when to use which.
vkarpov15 commentedon Jan 12, 2020
We'll add one. TLDR; you should use queries where possible. Aggregations are more powerful, but they bypass a lot of Mongoose's casting and validation, and they're harder to debug performance wise.