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

时间:2019-12-26 18:31:05

标签: mongodb robo3t

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

我使用MongoDB,试图获取带注释的文本,其注释为DATA NOT FOUND。 通过我的查询,我得到的所有记录的备注都为DATA NOT FOUND以及备注为TOO_MANY_DATA。 请参考数据库中存在的以下数据-

输入-

{
    "_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({'divisionIn.sections.labels.remarks':'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"
}

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

1 个答案:

答案 0 :(得分:1)

这是MongoDB的一种标准且可理解的数组错误观念。查询条件将产生范围为 document 的适当结果,而不必 just 您要查找的数组中的项目。换句话说,给定您希望找到DATA NOT FOUND的目标,大多数简单查询都会找到数组中至少一项匹配的任何文档-但不会过滤掉不匹配的文档。您必须稍微复杂一点才能做到这一点:

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"] }
                    }}
            }
            }}
    }}

                       ]);