简体   繁体   中英

Find where relation not null [Mongoose]

I have function like this

recommendModel.find({
        userId: userId
    })
            .populate('recommendedBy',null,{role: '4edd40c86762e0fb12000002'})
            .exec(function....);

This part role: '4edd40c86762e0fb12000002'} if inside recommendedBy no role like this 4edd40c86762e0fb12000002 it will return null like this:

  {
      "_id": "579a1600ce5b012224ba0d36",
      "recommendedBy": null,
      "userId": "5798a398e7dc3b242e1f38a5",
      "__v": 0
    }

I need if recommendedBy is null to not show record, is that possible?

Thanks in advance

On your query add the .where('recommendedBy').ne(null) . This will tell the query to (in your case find()) find() all matching documents "where" recommendedBy "is-not-equal-to" null .

BTW .ne() means not equal.

So in your case :

recommendModel.find({userId: userId})
    .where('recommendedBy').ne(null)
    .populate(...) // do your population
    .exec(...); // exec

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM