Hikaru Shindo
Hikaru Shindo

Reputation: 2913

Condition in populate() is not working

I want to query Image table with specified user but my code is not working.

Image.find().populate('user', { id : '1' }).sort({ updatedAt: 'desc' }).exec(function(err, images){
  console.log(images)
});

It still showed with all users.

How to query it? Thanks.

Upvotes: 1

Views: 93

Answers (2)

Hikaru Shindo
Hikaru Shindo

Reputation: 2913

@Ryan show me that I can do like this,

Image.find({ user: '1' }).populate('user').sort({ updatedAt: 'desc' }).exec(function(err, images){
  console.log(images)
});

Thanks a lot it's working.

But How about I want to query by user status like this ?

Image.find().populate('user', { status : 'ban' }).sort({ updatedAt: 'desc' }).exec(function(err, images){
  console.log(images)
});

It's not working. It showed me with all user.

How should I do ?

Upvotes: 1

Ryan W
Ryan W

Reputation: 6173

The populate method is used to fetch the related data of your model

you should modify your query like following

Image.find({ user: '1' }).populate('user').sort({ updatedAt: 'desc' }).exec(function(err, images){
  console.log(images)
});

Upvotes: 1

Related Questions