MongoDB-获取精确的数组元素,不包括其他元素

MongoDB-获取精确的数组元素,不包括其他元素,mongodb,robo3t,Mongodb,Robo3t,我是MongoDB新手,正在尝试执行查询以从数据库中查找匹配的文本。以下是提到的细节- 使用MongoDB,我试图获取带有注释的文本,并将其作为未找到的数据。 通过我的查询,我得到了所有的记录,这些记录的备注作为未找到的数据,以及备注作为过多的数据。 请参考数据库中的以下数据- 输入- { "_id" : ObjectId("aaaaaaaaaaaa"), "projectDR" : "123456789", "code" : "RRR", "fileName"

我是MongoDB新手,正在尝试执行查询以从数据库中查找匹配的文本。以下是提到的细节-

使用MongoDB,我试图获取带有注释的文本,并将其作为未找到的数据。 通过我的查询,我得到了所有的记录,这些记录的备注作为未找到的数据,以及备注作为过多的数据。 请参考数据库中的以下数据-

输入-

{
    "_id" : ObjectId("aaaaaaaaaaaa"),
    "projectDR" : "123456789",
    "code" : "RRR",
    "fileName" : "123456789_1.xml",
    "specFileDivNumber" : "050000",
    "normalizationStatus" : "ASDWFGL",
    "divisionIn" : {
        "sections" : [ 
            {
                "sectionNumber" : "050000",
                "sectionName" : "textile",
                "labels" : [ 
                    {
                        "normalizedDate" : ISODate("2018-10-28"),
                        "remarks" : "DATA NOT FOUND",
                        "bod" : false,
                        "ID" : "4048",
                        "annotatedText" : "Mains"
                    }, 
                    {
                        "normalizedDate" : ISODate("2018-10-28"),
                        "remarks" : "DATA NOT FOUND",
                        "bod" : false,
                        "ID" : "4064",
                        "annotatedText" : "routong"
                    }, 
                    {
                        "prefCode" : "ABC00000890",
                        "prefLabel" : "ABCRTYYUUUU",
                        "normalizedDate" : ISODate("2018-10-28"),
                        "remarks" : "TOO_MANY_DATA",
                        "bod" : false,
                        "ID" : "15736",
                        "annotatedText" : "Uniform"
                    },

                ]
            }
        ]
    },
    "status" : "Success",
    "fileDate" : ISODate("2018-10-28"),
    "Type" : "History"
}
查询-
db.getCollection('BasicInfo').find({'division.sections.labels.comments':'DATA NOT FOUND'))

预期产出:

{
    "_id" : ObjectId("aaaaaaaaaaaa"),
    "projectDR" : "123456789",
    "code" : "RRR",
    "fileName" : "123456789_1.xml",
    "specFileDivNumber" : "050000",
    "normalizationStatus" : "ASDWFGL",
    "divisionIn" : {
        "sections" : [ 
            {
                "sectionNumber" : "050000",
                "sectionName" : "textile",
                "labels" : [ 
                    {
                        "normalizedDate" : ISODate("2018-10-28"),
                        "remarks" : "DATA NOT FOUND",
                        "bod" : false,
                        "ID" : "4048",
                        "annotatedText" : "Mains"
                    }, 
                    {
                        "normalizedDate" : ISODate("2018-10-28"),
                        "remarks" : "DATA NOT FOUND",
                        "bod" : false,
                        "ID" : "4064",
                        "annotatedText" : "routong"
                    },                    
                ]
            }
        ]
    },
    "status" : "Success",
    "fileDate" : ISODate("2018-10-28"),
    "Type" : "History"
}

请帮助我更正查询,以获得预期的结果。

这是MongoDB的一个标准且可以理解的数组错误概念。查询条件将产生适用于文档的适当结果,而不一定只是要查找的数组中的项。换句话说,如果您希望找到
未找到的数据
,大多数简单的查询都会找到数组中至少有一项匹配的文档,但不会过滤掉那些不匹配的文档。要在一次拍摄中完成此操作,您必须稍微复杂一些:

db.foo.aggregate([
// Make sure at *least* one label has a remark of DATA NOT FOUND;
// otherwise, the $filter trick in the next stage yields a labels array
// of length 0 (which is not horrible).  Also, this is a good place to
// add other $match criteria, possibly index-optimized, to shrink down the
// size of response set:
{$match: {"divisionIn.sections.labels.remarks":"DATA NOT FOUND"}}

,{$project: {
        // Copy over the main doc level things we want:
        projectDR: "$projectDR",
        code: "$code",
        status: "$status"

        // divisionIn is a map, not an array, so we can dive down using dot notation
        // and make a new sections array called divSections that will ONLY have
        // DATA NOT FOUND: 
        divSections: {$map: {input: "$divisionIn.sections", as:"z", in:
            {
                // Again, copy over things we want to keep; may not need all of them
                "sectionNumber": "$$z.sectionNumber",
                "sectionName": "$$z.sectionName",

                // The Juice: Copy BUT FILTER the labels field conditionally based on
                // the value of labels.remarks:
                "labels": {$filter: {input: "$$z.labels",
                             as: "z2",
                             cond: {$eq: [ "$$z2.remarks", "DATA NOT FOUND"] }
                    }}
            }
            }}
    }}

                       ]);