These are chat archives for Automattic/mongoose

19th
Jul 2018
Simon Bailey
@thebailers
Jul 19 2018 10:07
This message was deleted
Adam Bohannon
@abohannon
Jul 19 2018 17:53
Am I able to set a limit on my query inside the query object itself? Or does the method need to be chained? Someone suggested I do the following (and I've seen some SO posts with similar code), but it's not working as expected:
Log.find({ userId, limit: 3 }, (err, logData) => {
      if (err) return res.json({ error: err })

      return res.json(logData)

    })
Kev
@lineus
Jul 19 2018 18:09
Adam, you can probably pass the limit in the 3rd parameter to find, the options object. I’m Mobile at the moment or I would test it out.
Adam Bohannon
@abohannon
Jul 19 2018 18:33
@lineus thanks for the reply, i did the following, but I'll try out the options object for future usecases
Log.find(query)
      .limit(+limit)
      .exec((err, data) => {
        if (err) return res.json({ error: err })

        return res.json(data)
      })
Brenda
@thehme
Jul 19 2018 20:36
@lineus thanks for the reply, per my question at Automattic/mongoose#1204, you were asking what version of mongodb we use. Unfortunately, we are still on version 2.6.12
Kev
@lineus
Jul 19 2018 20:37
you're welcome! I meant to ask which version of mongoose you use :smile:
@thehme ^
Brenda
@thehme
Jul 19 2018 20:40
@lineus mongoose version 4.11.7
Kev
@lineus
Jul 19 2018 20:41
ok thanks. I'll try and reproduce what you're seeing based on your SO question. I take it removing the square brackets didn't work?
Brenda
@thehme
Jul 19 2018 20:43
@lineus I tried the following, per http://mongoosejs.com/docs/4.x/docs/faq.html, but it did not work either:
p.dT[index].ts[i].th = newValue;
p.markModified("p.dT[index].ts[i].th");
p.save();
Brenda
@thehme
Jul 19 2018 20:57
@lineus i should also clarify that it does not work in my node script, but if I update a document directly, using robomongo and the same logic, it does work, e.g.
db.getCollection('p').update({'dT.name': 'summary', pid: 111 }, { '$set': { 'dT.0.ts.0.th': 80}})
Kev
@lineus
Jul 19 2018 21:07
@thehme I created a standalone example here that works for me in mongoose 5, I don't see why it wouldn't in 4, but I'll test it now.
the field in your update string should wind up looking like { $set: 'nested.array.1.innerarray.2.property': 'somevalue' }
Kev
@lineus
Jul 19 2018 21:15
it works for me with 4.11.7 ( that being said, I'm running mongodb 3.0.15 ( the oldest version i have on hand ) so ymmv on 2.6.12 :)
Brenda
@thehme
Jul 19 2018 21:28
@lineus thanks for checking this out, I am looking at your examples. I do know my version of mongodb is limited when it comes to the ease in which one can edit deeply embedded fields, since this only started being supported after 3.5.12 (https://jira.mongodb.org/browse/SERVER-831)
Kev
@lineus
Jul 19 2018 21:31
the only things that are really different between my two examples are the connection options and setting the Promise explicitly. The rest is the same. The positional operators are cool, I'm lucky enough to have come along late enough (3.6 was my first version) to have been using them from the start.