简体   繁体   中英

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.

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....
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM