These are chat archives for Automattic/mongoose

17th
May 2018
Sachin Srinivasan
@s8sachin
May 17 2018 09:14

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

Child.find({})
  .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 ?

Kev
@lineus
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
@RubyRonin
May 17 2018 09:22
what is the proper way to use the $and operator in mongo, like https://gist.github.com/RubyRonin/983bfb4c0063bcdbbfe4e31d391deaa0
I don't understand why that query doesn't work
I tried running it in https://mongoplayground.net/ and it fails
Sachin Srinivasan
@s8sachin
May 17 2018 09:30
@lineus thanks a lot :+1:
anytime :smile:
Sachin Srinivasan
@s8sachin
May 17 2018 09:36
cool :smile:
Sedric Heidarizarei
@saeedhei
May 17 2018 09:38
Hellow, How Can I backup my database or Collections with a node.js Script?
Kev
@lineus
May 17 2018 09:49

@RubyRonin instead of

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

try

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