【发布时间】:2012-06-28 17:59:15
【问题描述】:
我的 MongoDB 文档有结构:
{
_id : "12345",
name : "foo",
object : {
array_of_objects : [{
id : 507,
name : "some name",
dt : "2012-06-27 16:35:50"
},{
id : 506
name : "some other name",
dt : "2012-06-21 16:09:05"
},
…
]
}
}
我需要从 array_of_objects 中获取一个对象,该对象具有指定 name 的文档的特定 ID。我使用php并尝试执行下一个代码:
$collection->find(array('name' => 'foo', 'object.array_of_objects.id' => 507));
它返回 array_of_objects 的所有元素,而不是 id 507 的元素。 之后我尝试使用 $elemMatch 进行查询:
$collection->find(array('name' => 'foo', 'object.array_of_objects' => array('$elemMatch' => array('id' => 507))));
但它的返回值相同。 :( 我的 MongoDB 版本 2.0.6。请帮忙。
【问题讨论】:
-
MongoDB 的缺点:stackoverflow.com/questions/10042097/…
-
@david-cheung 谢谢!所以,我会尝试用 Mapreduce 来做到这一点。