ankitbaid11326
ankitbaid11326

Reputation: 71

MongoError $in needs an array

i am using trying to get data from an array and want to pass it to my find query inside $in operator. But its giving an error saying

$in needs an array

const fileIds = _.get(result, 'files', []);
console.log(fileIds);
db.collection('files').find({_id: {$in: fileIds}}).toArray((err, files) => 

{
    // some statements....
}

here is the output of console.log(fileIds)

{ '0': 5a8f24ab281bd22cd940530b, '1': 5a8f24ab281bd22cd940530c }

before marking to duplicate i have seen this post.

How to return the ObjectId or _id of an document in MongoDB? and error "$in needs an array"

but these aren't helping. so if anyone knows the answer please help. Thanks in advance.

Upvotes: 3

Views: 12586

Answers (1)

Manjeet Singh
Manjeet Singh

Reputation: 2398

Please do this

here i have use Object.values which is supported by node version 6.9 or 8 you can create array by another method

  const fileIds = _.get(result, 'files', []);

    db.collection('files').find({_id: {$in: Object.values(fileIds)}}).toArray((err, files) => 

    {
        // some statements....
    }

Upvotes: 3

Related Questions