2015-04-05 64 views
0

这是一个包含2个json文件的单个集合。我正在寻找一个特定的字段:对象中的值,如果匹配,必须返回整个子文档(集合中的该特定子文档必须从以下集合中的2个子文档中返回)。提前致谢。当(字段,值)匹配时,MongoDB find()返回子文档

{ 
"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", 
} 
+0

你有哪些特定字段/值和子文档的例子吗? – chridam 2015-04-05 06:59:35

+0

我已经使用此模式进行搜索(“clinical_study。@ rank”,“379”);并且它匹配但无法取回它所在的子文档。 – Vamshi 2015-04-05 07:52:27

+0

当你在mongo shell中执行这个查询时得到了什么结果db.TargetCollection.find({“clinical_study。@ rank”:“379”}) – kevin 2015-04-05 11:31:58

回答

0

你的榜样文件是畸形的 - 现在都clinical_study密钥是相同对象的一部分,而该对象缺少右}。我假设你希望它们成为两个单独的文档,尽管你称之为子文档。将它们作为文档的子文档(如果它们都使用同一个关键字命名)是没有意义的。您不能保存文档的方式,并在蒙戈外壳它会悄悄地与第二替换键的第一个实例:

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

如果你只是想,当你匹配到返回文档的clinical_study部分[email protected],使用投影:

db.test.find({ "[email protected]" : "379" }, { "clinical_study" : 1, "_id" : 0 }) 

相反,如果你意为clinical_study文件是一个更大的文档内的阵列元素,那么使用$。在这里,clinical_study现在是具有作为其元素在你的非文件clinical_study关键的两个值的数组字段的名称:

db.test.find({ "[email protected]" : "379" }, { "_id" : 0, "clinical_study.$" : 1 }) 
相关问题