2014-04-10 25 views
0

我有這樣找到一個數組的鍵記錄在文檔中

{ 
    "_id": "test1", 
    "friend": { 
    "test2": { 
     "name": "ax", 
     "level": 1, 
     "intimacy": 0, 
     "status": 0 
    }, 
    "test3": { 
     "name": "es", 
     "level": 1, 
     "intimacy": 0, 
     "status": 0 
    } 
    } 
}, 
{ 
    "_id": "test2", 
    "friend": { 
    "test2": { 
     "name": "ff", 
     "level": 1, 
     "intimacy": 0, 
     "status": 0 
    }, 
    "test3": { 
     "name": "dd", 
     "level": 1, 
     "intimacy": 0, 
     "status": 0 
    } 
    } 
} 

我需要找到friend.test2的節點我我想知道我改變數據_id = test1的 的文檔中的一些記錄結構來解決這個問題。任何人都可以幫助我。 結果可能是這樣的

{ 
    "test2": { 
    "name": "ax", 
    "level": 1, 
    "intimacy": 0, 
    "status": 0 
    } 
} 
+0

然後你有問題,如果你不想改變結構。你所問的也很不清楚。結果中的數據來自哪裏? –

+0

如果你的意思是幫助我,那麼你必須展示你做了什麼。否則改爲:有人爲我做我的工作。 –

+0

@SalvadorDali我沒有意識到這句話看起來不友好,只是因爲stackoverflow有一個字數限制。我不知道該寫什麼。對不起我的英文不好。 – chatfeed

回答

1

請嘗試下面的查詢。

db.<coll>.find({ _id:"test1" }, { "friend.test2":1 }) 
+1

我使用php。我試過findOne(array(「_ id」=>'test1','friend.test2'=> 1))。它不起作用。 – chatfeed

+1

答案是正確的,您需要先查找_id'test1',然後在'friend.test2'上做一個投影。讀取python mongo驅動程序文件,尋找投影 – anvarik

+0

嘗試$ collection-> find(array(「_ id」=>「test1」),array(「friend.test2」=> true)) –

0

在蒙戈外殼,你可以這樣做:

db.collection.find({_id:"test1"},{"friend.test2":1}) 

和PHP您可以嘗試像

$collection->find(array("_id"=>"test1"),array("friend.test2"=>true)) 
0

下面的代碼工作正常。測試。

$results = $coll->find(array(), array("_id"=>"test1"),array("friend.test2"=> 1)); 

foreach($results as $test) { print_r($test); }

相關問題