Hernan Humaña
Hernan Humaña

Reputation: 433

find data with arrays of ids in mongodb?

I have a array list from another example but this don’t work for me, maybe is because i need use ObjectId()’ but i don’t know how i will can parse this array, too i don’t know if is possibly get only once param (username) and not the full array?

the code is this:

var list =      ["5883d387971bb840b7399130","5883d389971bb840b7399131","5883d38a971bb840b7399132"]
.find({ _id: {$in : list}},{username:1})

can you help and say me how i need create the query ? 🙏🏻 thanks.

Upvotes: 0

Views: 33

Answers (1)

Helping hand
Helping hand

Reputation: 2920

You query is correct except you need to wrap your ids inside ObjectId using map.

var ObjectId = require('mongodb').ObjectID

var list = ["5883d387971bb840b7399130","5883d389971bb840b7399131","5883d38a971bb840b7399132"]

db.collectionName('newCollection').find({ _id: {$in : list.map( (id) =>  ObjectId(id))}}, {userName:1, _id:0})

Upvotes: 1

Related Questions