MongoDB find() 到 return 匹配 (field,value) 时的子文档
MongoDB find() to return the sub document when a (field,value) is matched
这是一个包含 2 个 json 文件的集合。我正在搜索一个特定的字段:对象中的值和整个子文档必须在匹配的情况下返回(该集合中的特定子文档必须从以下集合中的 2 个子文档中返回)。提前致谢。
{
"clinical_study": {
"@rank": "379",
"#comment": [],
"required_header": {
"download_date": "ClinicalTrials.gov processed this data on March 18, 2015",
"link_text": "Link to the current ClinicalTrials.gov record.",
"url": "http://clinicaltrials.gov/show/NCT00000738"
},
"id_info": {
"org_study_id": "ACTG 162",
"secondary_id": "11137",
"nct_id": "NCT00000738"
},
"brief_title": "Randomized, Double-Blind, Placebo-Controlled Trial of Nimodipine for the Neurological Manifestations of HIV-1",
"official_title": "Randomized, Double-Blind, Placebo-Controlled Trial of Nimodipine for the Neurological Manifestations of HIV-1",
}
{
"clinical_study": {
"@rank": "381",
"#comment": [],
"required_header": {
"download_date": "ClinicalTrials.gov processed this data on March 18, 2015",
"link_text": "Link to the current ClinicalTrials.gov record.",
"url": "http://clinicaltrials.gov/show/NCT00001292"
},
"id_info": {
"org_study_id": "920106",
"secondary_id": "92-C-0106",
"nct_id": "NCT00001292"
},
"brief_title": "Study of Scaling Disorders and Other Inherited Skin Diseases",
"official_title": "Clinical and Genetic Studies of the Scaling Disorders and Other Selected Genodermatoses",
}
您的示例文档格式不正确 - 现在 clinical_study
键是同一对象的一部分,并且该对象缺少结束符 }
。我假设您希望它们成为两个单独的文档,尽管您称它们为子文档。如果它们都在同一个键下命名,那么将它们作为文档的子文档是没有意义的。您不能以这种方式保存文档,并且在 mongo shell 中,它会默默地用第二个替换密钥的第一个实例:
> var x = { "a" : 1, "a" : 2 }
> x
{ "a" : 2 }
如果在 clinical_study.@rank
上匹配时只想 return 文档的 clinical_study
部分,请使用投影:
db.test.find({ "clinical_study.@rank" : "379" }, { "clinical_study" : 1, "_id" : 0 })
如果您的意思是将 clinical_study
文档作为较大文档中数组的元素,则使用 $
。在这里,clinical_study
现在是一个数组字段的名称,它的元素是非文档中 clinical_study
键的两个值:
db.test.find({ "clinical_study.@rank" : "379" }, { "_id" : 0, "clinical_study.$" : 1 })
这是一个包含 2 个 json 文件的集合。我正在搜索一个特定的字段:对象中的值和整个子文档必须在匹配的情况下返回(该集合中的特定子文档必须从以下集合中的 2 个子文档中返回)。提前致谢。
{
"clinical_study": {
"@rank": "379",
"#comment": [],
"required_header": {
"download_date": "ClinicalTrials.gov processed this data on March 18, 2015",
"link_text": "Link to the current ClinicalTrials.gov record.",
"url": "http://clinicaltrials.gov/show/NCT00000738"
},
"id_info": {
"org_study_id": "ACTG 162",
"secondary_id": "11137",
"nct_id": "NCT00000738"
},
"brief_title": "Randomized, Double-Blind, Placebo-Controlled Trial of Nimodipine for the Neurological Manifestations of HIV-1",
"official_title": "Randomized, Double-Blind, Placebo-Controlled Trial of Nimodipine for the Neurological Manifestations of HIV-1",
}
{
"clinical_study": {
"@rank": "381",
"#comment": [],
"required_header": {
"download_date": "ClinicalTrials.gov processed this data on March 18, 2015",
"link_text": "Link to the current ClinicalTrials.gov record.",
"url": "http://clinicaltrials.gov/show/NCT00001292"
},
"id_info": {
"org_study_id": "920106",
"secondary_id": "92-C-0106",
"nct_id": "NCT00001292"
},
"brief_title": "Study of Scaling Disorders and Other Inherited Skin Diseases",
"official_title": "Clinical and Genetic Studies of the Scaling Disorders and Other Selected Genodermatoses",
}
您的示例文档格式不正确 - 现在 clinical_study
键是同一对象的一部分,并且该对象缺少结束符 }
。我假设您希望它们成为两个单独的文档,尽管您称它们为子文档。如果它们都在同一个键下命名,那么将它们作为文档的子文档是没有意义的。您不能以这种方式保存文档,并且在 mongo shell 中,它会默默地用第二个替换密钥的第一个实例:
> var x = { "a" : 1, "a" : 2 }
> x
{ "a" : 2 }
如果在 clinical_study.@rank
上匹配时只想 return 文档的 clinical_study
部分,请使用投影:
db.test.find({ "clinical_study.@rank" : "379" }, { "clinical_study" : 1, "_id" : 0 })
如果您的意思是将 clinical_study
文档作为较大文档中数组的元素,则使用 $
。在这里,clinical_study
现在是一个数组字段的名称,它的元素是非文档中 clinical_study
键的两个值:
db.test.find({ "clinical_study.@rank" : "379" }, { "_id" : 0, "clinical_study.$" : 1 })