Reputation: 305
I have this structure:
{
"_id": NumberInt(101),
"link_id": {
"125": {
"thumb_position": NumberInt(1),
"last_scan": NumberInt(1234563645),
"row_numb": NumberInt(301),
"clicks": NumberInt(120)
},
"126": {
"thumb_position": NumberInt(2),
"last_scan": NumberInt(-2147483648),
"row_numb": NumberInt(1301),
"clicks": NumberInt(199)
},
{
...
}
}
}
the thing is I want to query by _id and link_id. I've tried in PHP:
$arr_ = array("_id" => intval(101), "link_id" => "125");
$res = $collection->findOne($arr_);
and many other variant and no results. If I only search for the _id everything works. Any ideas? Thx a lot!!
Upvotes: 0
Views: 107
Reputation: 4975
Since the link_id 125 is not a value I see two options:
a) Use $exists operator:
$arr_ = array(
"_id" => intval(101),
"link_id" => array("125" => array('$exists' => true))
);
b) Change your structure and use a slightly different query:
{
"_id": NumberInt(101),
"link": [
{
"id": "125",
"thumb_position": NumberInt(1),
"last_scan": NumberInt(1234563645),
"row_numb": NumberInt(301),
"clicks": NumberInt(120)
},{
"id": "126",
…
}
]
}
…
$arr_ = array(
"_id" => intval(101),
"link" => array("id" => "125")
);
Not tested but it should work in a similar way.
Upvotes: 1
Reputation: 43884
Hmm, a better schema here is to convert it to an array of objects rather than an object of objects, however, to answer:
$arr_ = array("_id" => intval(101), "link_id.125" => array('$exists' => true));
This will check for where that element exists as a key for the link_id
subdocuments using $exists
: http://docs.mongodb.org/manual/reference/operator/exists/
Upvotes: 0