0

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

1 回答 1

0

您的示例文档格式错误 - 现在两个clinical_study键都是同一个对象的一部分,并且该对象缺少一个 close }。我假设您希望它们成为两个单独的文档,尽管您称它们为子文档。如果它们都在同一个键下命名,那么将它们作为文档的子文档是没有意义的。您不能以这种方式保存文档,并且在 mongo shell 中,它会默默地将密钥的第一个实例替换为第二个:

> var x = { "a" : 1, "a" : 2 }
> x
{ "a" : 2 }

如果您只想clinical_study在匹配 on 时返回文档的一部分clinical_study.@rank,请使用投影:

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 })
于 2015-04-06T16:02:22.993 回答