MongoDB-获取确切的数组元素,不包括其他元素 [英] MongoDB- Fetching exact array element, excluding others
问题描述
我是MongoDB的新手,正在尝试执行查询以从数据库中找到匹配的文本.下面是提到的细节-
I am new to MongoDB and trying to execute a query to find a matching text from a database. Below are the details mentioned -
使用MongoDB,我试图获取带注释的标注为DATA NOT FOUND的文本. 通过我的查询,我得到的所有记录的备注都为DATA NOT FOUND以及备注为TOO_MANY_DATA. 请参考数据库中存在的以下数据-
Using MongoDB, I am trying to fetch the annotated texts having remarks as DATA NOT FOUND. With my query, I am getting all the records having remarks as DATA NOT FOUND as well as remarks as TOO_MANY_DATA. Please refer to the below data present in the database-
输入-
{
"_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"
}
请帮助我更正查询,以便获得期望的结果.
Please help me to correct the query so as to get the expected.
推荐答案
这是对MongoDB的一种标准且可理解的数组错误观念.查询条件将产生范围为 document 的适当结果,而不必 just 您要查找的数组中的项目.换句话说,给定您希望找到DATA NOT FOUND
的目标,大多数简单查询都会找到数组中至少一项匹配的任何文档-但不会过滤掉不匹配的文档.您必须稍微复杂一点才能做到这一点:
This is a standard and understandable array-of-array misconception with MongoDB. The query criteria will yield the proper result scoped to a document, not necessarily just the items in an array you are looking for. In other words, given your desired goal of finding DATA NOT FOUND
, most simple queries will find any document where at least one item in the array matches -- but will not filter out those that do not. You have to get a little more complex to do this in one shot:
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"] }
}}
}
}}
}}
]);
这篇关于MongoDB-获取确切的数组元素,不包括其他元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!