2

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.

ankitbaid11326
  • 61
  • 1
  • 2
  • 10

1 Answers1

2

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....
    }
Manjeet Thakur
  • 2,288
  • 1
  • 16
  • 35