Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 检查数组中是否存在所有元素_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 检查数组中是否存在所有元素

Node.js 检查数组中是否存在所有元素,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在尝试使用mongoose在MongoDB数据库的数组中查找数组的所有元素 如果我在MongoDB中的数据是- { row: "A", reserve: [1,2,3] } 如果我的查询数据是- { row: "A", arr: [1,2] // I want it to return result } 如果我的查询数据是 { row: "A", arr: [1,2,4] // I want it to return null } 我想更新数据,但每次都在更新

我正在尝试使用mongoose在MongoDB数据库的数组中查找数组的所有元素

如果我在MongoDB中的数据是-

{
  row: "A",
  reserve: [1,2,3]
}
如果我的查询数据是-

{
  row: "A",
  arr: [1,2] // I want it to return result
}
如果我的查询数据是

{
  row: "A",
  arr: [1,2,4] // I want it to return null
}
我想更新数据,但每次都在更新

Reserved.updateOne({row: "A", reserve: {$in: arr}, {
  $push: {
    reserve: arr
  }
}, (err, result) => {
  // ...
});

请帮助我。

您可以使用
$all
操作符()。比如:

db.collection.updateOne(
        {
          row: "A",
          reserve: { $all: array } 
        }
)
希望这有帮助

可以使用运算符匹配数组中的所有元素

从文件中

操作员选择字段值为的文档 包含所有指定元素的数组

甚至使用find查询

db.collection.find({ "row": "A", "reserve": { "$all": array }})

试试这个
db.collection.aggregate([{$project:{$reserve:{$filter:{input:$reserve],cond:{$in:[“$$this”,[1,2]]}}])
我想使用
findOne
因为我正在使用
updateOne
更新数据。我正在使用updateOne更新数据不清楚您在问什么请尝试此
db.collection.updateOne({row:“A”,reserve:{$all:arr}})
Thnx它有效。。
db.collection.find({ "row": "A", "reserve": { "$all": array }})