Find multiple ids in collection (one line query)

*name is unique

collection : {
[{
_id: ‘1’,
name: “Bob”,
},
{
_id: ‘2’,
name: “sara”,
},
{
_id: ‘3’,
name: “jon”,
},
],
}
arrayToFind = [“Bob”,“sara”]

need to find all matching names in one call.

expected result => {
_id: ‘1’,
name: “Bob”,
},
{
_id: ‘2’,
name: “sara”,
},

Please enjoy the fine documentation of $in.

1 Like

$in returns only one doc and not all of them

In does not. See the many examples in from the documentation.

Or this one:

mongosh> c.find()
{ _id: '1', name: 'Bob' }
{ _id: '2', name: 'sara' }
{ _id: '3', name: 'jon' }
mongosh> arrayToFind = [ "Bob" , "sara" ]
mongosh> c.find( { "name" : { "$in" : arrayToFind } } )
{ _id: '1', name: 'Bob' }
{ _id: '2', name: 'sara' }
2 Likes

thanks I find the bug in my code

1 Like

Please share so others do not fall on the same trap.

2 Likes