首页 文章

MongoError $ in需要一个数组

提问于
浏览
1

我正在尝试从数组中获取数据,并希望将其传递给$ in运算符内的查询查询 . 但它给出了一个错误的说法

$ in需要一个数组

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

{
    // some statements....
}

这是console.log(fileIds)的输出

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

在标记复制之前我已经看过这篇文章 .

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

但这些都没有帮助 . 所以,如果有人知道答案,请帮助 . 提前致谢 .

1 回答

  • 0

    请在此处执行此操作我使用了节点版本6.9或8支持的Object.values,您可以通过其他方法创建数组

    const fileIds = _.get(result, 'files', []);
    
        db.collection('files').find({_id: {$in: Object.values(fileIds)}}).toArray((err, files) => 
    
        {
            // some statements....
        }
    

相关问题