Reputation: 190
I have a student collection like below,
{_id:22222, student_id:1}
{_id:22223, student_id:2}
{_id:22224, student_id:3}
{_id:22225, student_id:4}
I have an array like [1,2], and I want to return only the documents that as the same student_id in the array.
if the array is [1,3], the output should be also in an array [1,3]
if I have array like [4,7], and since 7 is not a valid student_id, it should return only [4]
Upvotes: 0
Views: 41
Reputation: 1080
Use with aggregate with match keyword
db.Signal.aggregate(
{$match:{student_id:{$in:[4,3]}}},
{$project:{_id:0,'student_id':1}},
{ $group: { _id: null, student: { $push:'$student_id' } } }
)
Upvotes: 1
Reputation: 59557
Use the $in operator:
db.students.find({
student_id: { $in: [1, 7] }
})
Upvotes: 1