Sunday, 16 April 2017

return subdocuments from array of id's with mongoose

I currently have an array of multiple id's which reference subdocuments. What I need to do is search collections for these Id's and return the information associated with them.

var ids =['58c2871414cd3d209abf4fc0','58c2871414cd3d209abf5fc0'];
User.aggregate(
      {$unwind : "$position"},
      {$match:{'_id': { $in: [

     // need to reference the ids array here

    ]}
}
}, function(err, docs){
     console.log(docs);
});

});

My user schema is as follows:

var User = new Schema({
id: String,
name: String,
position: [{
          _id:String,
          title: String,
          location: String,
          start: String,
          term:Number,
          description:String,
          date: {type: Date, default: Date.now},

So basically what I need to do is return the entire Position subdocument if it matches an id from the array. Any Ideas?



via user

No comments:

Post a Comment