简体   繁体   中英

Mongodb + PHP => Find query operator in multidimensional array

I need to find in array saved in a document of Mongodb.

Here is the document :

{

 "hashtag" : "World",
 "topimages" : [ 
        {
            "cluster" : "france",
            "id_tw" : "477170636327227393"
        },
        {
            "cluster" : "france",
            "id_tw" : "477170636327227396"
        }

  ]

}

And I want to search if a document named "World" by key "hashtag", have already a tweet saved in the array "topimages" with an id "id_tw" of value : "477170636327227393"

What I'm trying to do is :

$query = array('hashtag' => "World", array('topsimages.$.id_tw' => "477170636327227393"));
$xpmm->find($query);

I need to search if a document called "World" has already a tweet saved in the array "topimages" by his key "id_tw".

Thanks a lot.

#

Solution :

 $query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464"); 

topimages.x would only work if it was a subdocument, not an array. You want $elemMatch ( Mongo DB Documentation )

$query = array('hashtag' => "World", 'topsimages' => array("$elemMatch" => array("id_tw" => "477170636327227393")));

可以的:

$query = array('hashtag' => "france", 'topimages.id_tw' => "477170282852286464");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM