MongoDB匹配数组内的子文档(非位置引用)

MongoDB匹配数组内的子文档(非位置引用),mongodb,Mongodb,我的MongoDB有一个键值对结构,在我的文档中有一个data字段,它是一个数组,包含两个字段的许多子文档:name和value 如何搜索子文档,例如({“name”:“position”,“value”:“manager”})和多个子文档(例如{“name”:“age”,“value”:{$ge:30}) 编辑:我不是在寻找我在标题(不是位置引用)中提到的特定子文档,而是想检索整个文档,但我需要它精确匹配两个子文档。这里有两个查询来查找以下记录: { "_id" : ObjectId

我的MongoDB有一个键值对结构,在我的文档中有一个
data
字段,它是一个数组,包含两个字段的许多子文档:
name
value

如何搜索子文档,例如(
{“name”:“position”,“value”:“manager”}
)和多个子文档(例如
{“name”:“age”,“value”:{$ge:30}


编辑:我不是在寻找我在标题(不是位置引用)中提到的特定子文档,而是想检索整个文档,但我需要它精确匹配两个子文档。

这里有两个查询来查找以下记录:

{ 
    "_id" : ObjectId("sometobjectID"), 
    "data" : [
        {
            "name" : "position", 
            "value" : "manager"
        }
    ]
}

// Both value and name (in the same record):
db.demo.find({$elemMatch: {"value": "manager", "name":"position"}})


// Both value and name (not necessarily in the same record):
db.demo.find({"data.value": "manager", "data.name":"position"})


// Just value:    
db.demo.find({"data.value": "manager"})
请注意如何使用
,这适用于所有子文档,即使它们位于数组中

您可以在这里使用您喜欢的任何操作符,包括
$gte

编辑

由于@response,答案中添加了$elemMatch


解释
$elemMatch

之间的区别。可能重复的否,我不想找到特定的子文档,我想检索整个记录。但我的问题是如何使用数组中的子文档进行查询。使用
$elemMatch
查询变量谢谢@Veeram,如果我想查询多个elemMatch,是否使用$and运算符?Np。干得好。令人惊叹的。那样的话,@cherhan你介意投票回答吗?