Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose :Find and filter nested array

I'm trying to find a whole document with Find() command and filter a nested array with a condition.

Here a piece of the used Schema :

var ListSH = new Schema({
  name: { type: String, unique: true, required: true},
  subject : String,
  recipients : [
    Schema({
     uid : { type : ObjectId, required : true, ref:'User', unique: true},
     status : { type : Number, default : 1 }
     },{_id: false})
  ]
};

Currently I do ListModel.findOne({ _id : req.params.id_list, function(err,list){...};

And Postman give me that:

{
  "_id": "57e6bcab6b383120f0395aed",
  "name": "Emailing listname",
  "subject": "List subject",
  "recipients": [
    {
      "uid": "57e932bcbbf0e9e543def600",
      "status": 0
    },
    {
      "uid": "57e93266c3c0b1dc1625986f",
      "status": 1
    }
  ]
}

I'd like Postman to return me something like that by adding a recipients.status : 1 condition

  {
      "_id": "57e6bcab6b383120f0395aed",
      "name": "Emailing listname",
      "subject": "List subject",
      "recipients": [
        {
          "uid": "57e93266c3c0b1dc1625986f",
          "status": 1
        }
      ]
    }

I've already tried ListModel.findOne({ _id : req.params.id_list, 'recipients.status' : 1}, function(err,list){...};

and something weird like populate([$match('recipients.status : 1)]); but with no success..

Anyone knows ? Thanks ^^

like image 493
nicolas.grd Avatar asked Sep 26 '16 16:09

nicolas.grd


2 Answers

You can use aggregate to get it in an easy way like this

ListModel.aggregate(
    { $match: {_id: ObjectId("57e6bcab6b383120f0395aed")}},
    { $unwind: '$recipients'},
    { $match: {'recipients.status':1}})

Output

{
    "_id" : ObjectId("57e6bcab6b383120f0395aed"),
    "name" : "Emailing listname",
    "subject" : "List subject",
    "recipients" : {
        "uid" : "57e93266c3c0b1dc1625986f",
        "status" : 1
    }
}

To understand aggregation in details see the docs here

like image 193
abdulbarik Avatar answered Oct 14 '22 16:10

abdulbarik


Try below query:

ListModel.findOne({"_id" : "57e6bcab6b383120f0395aed", 'recipients.status' : 1},{_id:1, name: 1, subject:1,'recipients.$': 1}, function(err,list){...});
like image 31
Sachin Avatar answered Oct 14 '22 16:10

Sachin