2017-09-17 71 views
0

我想找到匹配例如 「conceptID」 所有文件:如何在MongoDB的文檔中查找與數組中某個鍵的值匹配的所有文檔?

{ 
    "_id" : ObjectId("59bc0bd77d7934a6a7243f05"), 
    "definitionID" : "0161-1#DF-000001#1", 
    "reference" : "FIIG=A23900 INC=62356", 
    "dummy1" : "", 
    "dummy2" : "", 
    "dummy3" : "", 
    "organisationID" : "0161-1#OG-002462#1", 
    "languageID" : "0161-1#LG-000001#1", 
    "statusTerm" : 0, 
    "definitions" : [ 
     { 
      "_id" : ObjectId("59bc34a67d7934a6a7c131cc"), 
      "definitionID" : "0161-1#DF-000001#1", 
      "conceptID" : "0161-1#01-000001#1", 
      "definition" : "A metallic claw shaped pivoting item, designed to accelerate the weapon's recovery from recoil by assisting in realigning the breech with the barrel." 
     } 
    ] 
} 

我已經嘗試過這種 「0161-1#01-000001#1」,但不工作:find({definitions[0].conceptID: "0161-1#01-000001#1"})

回答

1

如果你想在特定的索引搜索,那麼你需要在數組索引提供如下解決它。

find({"definitions.0.conceptID":"0161-1#01-000001#1"})

否則,對於單鍵搜索可以直接搜索使用

definitions.conceptID

0

通過使用$elemMatch

.find({definitions: { $elemMatch: {conceptID: "0161-1#01-000001#1"} }}) 
相關問題