These are chat archives for Automattic/mongoose

May 2018
Sachin Srinivasan
May 17 2018 09:14

Hello everyone,
please help me on how to sort() based on field populated using populate()
I tried this,

  .populate('father', 'number')
  .sort({'father.number': 'descending'})

im trying to sort child data based on father's number, it is not working..
Child model has field "father" with_idofFather` model.
data is being populated, but sort is not working.
is there any other way achieve this ?

May 17 2018 09:21
@s8sachin sort gets applied to the find query that gets sent to mongodb db, which effects the cursor returned from the find. Child.sort will be unaware of the number field which is actually in the Father collection's documents. You can perform your find.populate operation, then use Array.sort() on the return from find.
Ruby Ronin
May 17 2018 09:22
what is the proper way to use the $and operator in mongo, like
I don't understand why that query doesn't work
I tried running it in and it fails
Sachin Srinivasan
May 17 2018 09:30
@lineus thanks a lot :+1:
anytime :smile:
Sachin Srinivasan
May 17 2018 09:36
cool :smile:
Sedric Heidarizarei
May 17 2018 09:38
Hellow, How Can I backup my database or Collections with a node.js Script?
May 17 2018 09:49

@RubyRonin instead of

$and: [{ "count": 10 }, {"collection": "collection"}]


$and: [{ count: { $eq: 10 } }, {collection: { $eq: "collection" }}]