Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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 MongoDB:检查嵌套数组是否包含子数组_Node.js_Mongodb_Mongoose_Mongodb Query_Mongoose Schema - Fatal编程技术网

Node.js MongoDB:检查嵌套数组是否包含子数组

Node.js MongoDB:检查嵌套数组是否包含子数组,node.js,mongodb,mongoose,mongodb-query,mongoose-schema,Node.js,Mongodb,Mongoose,Mongodb Query,Mongoose Schema,我有一个用例,其中数据库建模如下: name: XYZ gradeCards: [{ id: 1234, // id of the report card comments: ['GOOD','NICE','WOW'] }, { id: 2345, comments: ['GOOD','NICE TRY'] }] 现在,我有一个查询,我想查询模式,如下所示: [{ id: 1234, comments: ['GOOD','NICE'] },{ id: 2345,

我有一个用例,其中数据库建模如下:

name: XYZ
gradeCards: [{
  id: 1234, // id of the report card
  comments: ['GOOD','NICE','WOW']
}, {
  id: 2345,
  comments: ['GOOD','NICE TRY']
}]
现在,我有一个查询,我想查询模式,如下所示:

[{
  id: 1234,
  comments: ['GOOD','NICE']
},{
  id: 2345,
  comments: ['GOOD']
}]
我会得到一个ID和值的列表。 例如:给定的列表如下所示:

[{
  id: 1234,
  comments: ['GOOD','NICE']
},{
  id: 2345,
  comments: ['GOOD']
}]
简言之,ID应该是匹配的,注释应该是该ID的注释数组的子数组,并且数组中指定的所有条件都应该匹配,因此它应该是提供的所有条件的and条件

我能够访问这个查询,但是它匹配comments数组中的所有元素,我希望id应该完全匹配,comments应该是子数组

要匹配注释数组中的所有元素,请执行以下操作:

db.getCollection('user').find({arr:{
    $all: [{
        id:1234,
        comments:['GOOD','NICE','WOW']
    },{
        id:2345,
        comments:['GOOD','NICE TRY']
    }]
 }})

您可以尝试使用
$elemMatch
匹配查询条件

db.collection.find({
    gradeCards: {
        $all: [{
            "$elemMatch": {
                id: 1234,
                comments: {
                    $in: ['GOOD', 'NICE']
                }
            }
        }, {
            "$elemMatch": {
                id: 2345,
                comments: {
                    $in: ['GOOD']
                }
            }
        }, ]
    }
})

谢谢你的回答,你能解释一下为什么我们需要$elemMatch吗?我可以使用$in。不客气
$elemMatch
在数组中至少匹配一个文档,该文档同时具有
id
注释
匹配查询值,并且
$all
$elemMatch
匹配两个文档各自的查询条件。还有,我现在拿到了。感谢您的帮助,您能否提供一些关于我们是否需要所有指定数组输入的OR条件而不是AND条件的想法?为什么不简单地用$in替换$all并删除$elemMatch不起作用呢?您必须使用
db.collection.find({“成绩卡”:{“$elemMatch”:{“$or”:[{id:1234,注释:{$in:['GOOD','NICE']},{id:2345,注释:{$in:['BAD']}}}})
用于
。您希望操作员以一定的方式工作,但他们未必如此。请阅读有关它们的文章。