Json MongoDB find()在匹配(字段、值)时返回子文档

Json MongoDB find()在匹配(字段、值)时返回子文档,json,mongodb,find,subdocument,Json,Mongodb,Find,Subdocument,这是一个包含2个json文件的集合。我正在搜索一个特定字段:对象中的值和整个子文档必须返回,如果匹配,则集合中的特定子文档必须从以下集合中的2个子文档中返回。提前谢谢 { "clinical_study": { "@rank": "379", "#comment": [], "required_header": { "download_date": "ClinicalTrials.gov processed this data on March 18, 2015", "link_text

这是一个包含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",
}

您的示例文档格式不正确-现在两个临床研究键都是同一对象的一部分,并且该对象缺少一个结束}。我假设您希望它们是两个独立的文档,尽管您称它们为子文档。如果它们都在同一个键下命名,那么将它们作为文档的子文档是没有意义的。您无法以这种方式保存文档,在mongo shell中,它将以静默方式将密钥的第一个实例替换为第二个:

> var x = { "a" : 1, "a" : 2 }
> x
{ "a" : 2 }
如果您只想在匹配临床研究时返回文档的临床研究部分。@排名,使用投影:

db.test.find({ "clinical_study.@rank" : "379" }, { "clinical_study" : 1, "_id" : 0 })
如果您想让临床研究文档成为更大文档中的数组元素,则使用$。这里,clinical_study现在是一个数组字段的名称,它的元素是非文档中clinical_study键的两个值:

db.test.find({ "clinical_study.@rank" : "379" }, { "_id" : 0, "clinical_study.$" : 1 })

你有哪一个字段/值和哪一个子文档的例子吗?我已经用这个模式临床研究进行了搜索。@rank,379;并且它是匹配的,但无法取回它所在的子文档。在mongo shell db.TargetCollection.find{clinical_study@rank:379}中执行此查询时,会得到什么结果?我取回的值与我搜索的值相同,而不是子文档