0

I want to query MongoDB using Mongoose with an array of objectIDs. I am not sure how to do this and also get back the 'name' property that is on the root of each document. I've looked online and can't seem to get it working. Below are two things I have tried that might be close but don't work.

{ _id: { 
    $in : ['5d193a4826540f7a89757f1d']
  }
}
{ "name" : { 
    id: { 
        $in : ['5d193a4826540f7a89757f1d', '5d8c104d0f867b753d1f506c']
    } 
}}

1 Answer 1

2

First make ObjectIds from your strings:

const ids = ['5d193a4826540f7a89757f1d', '5d8c104d0f867b753d1f506c'];
const queryIds = ids.map(item => ObjectId(item));

Then find needed documents of your collection and set name: 1 to get only name (and _id by default):

collection.find({"_id": {$in : queryIds}}, {name: 1});
Sign up to request clarification or add additional context in comments.

Comments

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.