我需要找到保存在Mongodb文档中的数组。
文件如下:
{
"hashtag" : "World",
"topimages" : [
{
"cluster" : "france",
"id_tw" : "477170636327227393"
},
{
"cluster" : "france",
"id_tw" : "477170636327227396"
}
]
}
我想搜索一个名为"World"的文档,通过键"hashtag",已经在数组"topimages"中保存了一个id为"id_tw"的值:"477170636327227393"
我要做的是:
$query = array('hashtag' => "World", array('topsimages.$.id_tw' => "477170636327227393"));
$xpmm->find($query);
我需要搜索一个名为"World"的文档是否已经通过他的键"id_tw"保存在数组"topimages"中。
谢谢你。
#解决方案:
$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464");
使用
$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464");
topimages。X只有在它是子文档而不是数组时才能工作。你想要$elemMatch
(Mongo DB Documentation)
$query = array('hashtag' => "World", 'topsimages' => array("$elemMatch" => array("id_tw" => "477170636327227393")));