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



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

使用 MongoDB,我正在尝试获取带有注释的文本,这些文本的注释为"未找到数据"。 通过我的查询,我得到了所有备注为"未找到数据"以及备注为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的一个标准且可以理解的数组对数组的误解。 查询条件将生成范围限定为文档的正确结果,而不一定只是要查找的数组中的项。 换句话说,给定查找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"] }
}}
}
}}
}}
]);

最新更新