These are chat archives for Automattic/mongoose

15th
Jul 2018
Adam Bohannon
@abohannon
Jul 15 2018 01:19
Hey all, I'm curious if you're able to use dot notation to edit documents? I used the following code in a Freecodecamp challenge and it passed, but I wasn't expecting it to because I didn't use $set. Been trying to find a SO thread or anything that discusses this, but can't seem to find one (nor in the docs themselves, unless I'm overlooking it).
var findAndUpdate = function(personName, done) {
  var ageToSet = 20;

  Person.findOneAndUpdate({ name: personName }, { new: true }, (err, data) => {
    if (err) done(err)

    data.age = ageToSet

    data.save((err, data) => {
      if (err) done(err)

      done(null, data)
    })

  })
};
panigrah
@panigrah
Jul 15 2018 07:39
@abohannon you were not expecting the data.age` statement to work?
Kev
@lineus
Jul 15 2018 11:48
@abohannon when you create a document ( with either new SomeModel(..) or as a return from a query ), for every path in the schema for that Model ( including the ones mongoose adds: _id, id, __v, and all nested paths ) mongoose defines a property on the document using Object.defineProperty(). When that property is defined, it is defined with a getter and a setter that calls .get() or .$set() respectively for you. The relevant source code is in node_modules/mongoose/lib/helpers/document/compile.js which is required by node_modules/mongoose/lib/document.js.
Irfan Raza
@irfaan008
Jul 15 2018 15:21

Hi @lineus , I want to get count of total documents matching my aggregation pipeline. My mongoose query are being built at runtime based on input params provided and I want to use the same to get the count as well as the result (with pagination applied).

It works perfectly when I do it using find()

const query = User.find({ deleted: false, role: role }).skip(10).limit(10) const countQuery = query.model.find().merge(query).skip(0).limit(0) const [users, count] = await Promise.all([query, countQuery.count()])

Any idea how can I use something similar for aggregate?

Kev
@lineus
Jul 15 2018 17:17
Hi @irfaan008 :smile: Before I answer your actual question, I think your query example is a perfect candidate for using Query.prototype.toConstructor API docs here.
note that the crappy script I wrote to make gists from code samples doesn't handle stderr yet, so the deprecation warning about count() being deprecated isn't included in the output
Irfan Raza
@irfaan008
Jul 15 2018 18:49

Thanks @lineus for the api doc. That is the official way I see now to get this done if I am using find().

But my question is how to achieve this when we have aggregate?
When I use it like this
Test.aggregate({ $match: { isActive: true } }).toConstructor();
It shows error that
UnhandledPromiseRejectionWarning: TypeError: Test.aggregate(...).toConstructor is not a function

This is what I am looking for actually.
Kev
@lineus
Jul 15 2018 18:51
sorry @irfaan008, I got pulled away and didn't get a chance to answer your question :) I'll be right back
Irfan Raza
@irfaan008
Jul 15 2018 18:51
:)
Kev
@lineus
Jul 15 2018 19:45
ok, I'm back. give me a few minutes to hash it out
Irfan Raza
@irfaan008
Jul 15 2018 19:45
Sure
Kev
@lineus
Jul 15 2018 20:19
it's not pretty, but this is what I came up with using aggregate the aggregate $facet stage allows you to handle the same input with multiple pipelines. So it can be done in one aggregation command.
I'm sure someone with more experience could do it in less lines :smile:
Irfan Raza
@irfaan008
Jul 15 2018 20:20
Okay, let me check that
Kev
@lineus
Jul 15 2018 20:20
I'm pretty sure I can (and will) given more time, I just didn't want to make you wait any longer :)
Irfan Raza
@irfaan008
Jul 15 2018 20:21
Can't we have similar method toConstructor() which we have in find()?
Kev
@lineus
Jul 15 2018 20:22
with queries, you have to run it twice, with aggregation you only need to run it once.
Irfan Raza
@irfaan008
Jul 15 2018 20:22
Hmmm... I see that.
Kev
@lineus
Jul 15 2018 20:25
I'm definitely still new to aggregate, I've only been doing this since January, so I AM NOT AN EXPERT YET :imp: I'm learning too!
Irfan Raza
@irfaan008
Jul 15 2018 20:26
Believe me you are still doing pretty well !!!