【问题标题】:MongoDB- Fetching exact array element, excluding othersMongoDB-获取精确的数组元素,不包括其他元素
【发布时间】:2019-12-27 22:53:35
【问题描述】:

我是 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"
}

请帮助我更正查询以达到预期。

【问题讨论】:

    标签: mongodb robo3t


    【解决方案1】:

    这是对 MongoDB 的一种标准且可以理解的数组数组误解。查询条件将产生范围为文档的正确结果,不一定只是您正在查找的数组中的项目。换句话说,给定您想要找到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"] }
                        }}
                }
                }}
        }}
    
                           ]);
    

    【讨论】:

      猜你喜欢
      • 2022-10-13
      • 2015-10-02
      • 2015-07-23
      • 1970-01-01
      • 2021-06-21
      • 2014-05-02
      • 2016-07-22
      • 2018-05-17
      • 2012-05-13
      相关资源
      最近更新 更多