Skip to content

Question: Aggregate vs. Query builder #8494

Closed
@thernstig

Description

@thernstig

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?

Activity

changed the title [-]Question: Aggregate vs. Query buildr[/-] [+]Question: Aggregate vs. Query builder[/+] on Jan 11, 2020
AbdelrahmanHafez

AbdelrahmanHafez commented on Jan 12, 2020

@AbdelrahmanHafez
Collaborator

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

thernstig commented on Jan 12, 2020

@thernstig
Author

@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

vkarpov15 commented on Jan 12, 2020

@vkarpov15
Collaborator

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.

added this to the 5.8.8 milestone on Jan 12, 2020
added
docsThis issue is due to a mistake or omission in the mongoosejs.com documentation
on Jan 12, 2020
modified the milestones: 5.8.8, 5.8.9 on Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vkarpov15@AbdelrahmanHafez@thernstig

        Issue actions

          Question: Aggregate vs. Query builder · Issue #8494 · Automattic/mongoose