0

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.

1 Answer 1

1

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})
Sign up to request clarification or add additional context in comments.

2 Comments

i have this error from node : “ObjectId is not defined” but i import from mongo and work !
var ObjectId = require('mongodb').ObjectID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.