2015-04-05 85 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 })