2017-09-17 70 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"} }}) 
相关问题