如何使用$all和数组作为参数来查询表

23 浏览
0 Comments

如何使用$all和数组作为参数来查询表

这个问题已经有了答案:

查找包含特定值的数组的文档

我想使用默认的 _id 查询一个名为 orders 的集合,并使用包含我需要的 _id 的数组,但是当我尝试将结果保存到另一个

let orderIds = ['example1', 'example2']
db.collection("orders").find({ _id: { $all: orderIds } }, function (err, orders) {
    console.log(orders);
})

admin 更改状态以发布 2023年5月20日
0
0 Comments

你需要使用 $in 而不是 $all 来获取给定ids数组中所有的文档

试试这个:

db.collection("orders").find({ _id: { $in: orderIds } }, function (err, orders) {
    console.log(orders);
})

了解更多关于$in的内容

0