mongoose find in array of ids code example

Example 1: find one with specofoc id mongoose

YOUR_MODEL_NAME.findOne({_id: reqParameterID}, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      // Do Something 
    }
  });

Example 2: mongodb findmany

model.find({
    '_id': { $in: [
        mongoose.Types.ObjectId('4ed3ede8844f0f351100000c'),
        mongoose.Types.ObjectId('4ed3f117a844e0471100000d'), 
        mongoose.Types.ObjectId('4ed3f18132f50c491100000e')
    ]}
}, function(err, docs){
     console.log(docs);
});

Example 3: find by array of ids mongoose

User.find({ _id: { $in: followedIDs } }, (err, verbs) => {});

Example 4: mongoose populate array of ids

Use the name of the schema path instead of the collection name:

Conversation.findOne({ _id: myConversationId})
.populate('recipients') // <==
.exec(function(err, conversation){
    //do stuff
});

Tags:

Go Example