0

I have a JSON of this form from MongoDB and I want to retrieve documents based on same queries:

 {
      "_id" : ObjectId("5281fb3070c68c0c1ccc4f00"),
      "channel" : "1",
      "content" : "8578",
      "duplicateOf" : null,
      "id" : "7420",
      "keywords" : [ObjectId("52816d3370c68c2c1c450500"), ObjectId("52816d3370c68c2c1c570500"), ObjectId("52816d3370c68c2c1c470500"), ObjectId("52816d3370c68c2c1c4d0500"), ObjectId("52816d3370c68c2c1c590500"), ObjectId("52816d3370c68c2c1c530500"), ObjectId("52816d3370c68c2c1c4f0500"), ObjectId("52816d3370c68c2c1c6b0500")],
      "lastUpdate" : "2009-11-16"]
    }

I use this query with MongoVue and it works :

   db.Measurements.find({ "_id" : 
{ "$in" : [ObjectId("5281fb3070c68c0c1ccc4f00"), ObjectId("5281fb3070c68c0c1cce4f00")] } })

but I don't find the way to make it work in PHP,here is the part of the code:

 foreach ($series as $doc) {

       $ids = $collectionMeasurements->find(array('_id' => array($in=>new MongoId($doc[keywords]))));
    }

Thanks for your help!!!

===================================================== Found the error!!!

 foreach ($series as $doc) {

 $ids = $collectionMeasurements->find(array('_id' => array(
           '$in'=>array(new MongoId(string)($doc[keywords])))));
    }

2 Answers 2

1

$in would be a variable in PHP. It needs to be a string (as it is in your MongoVue code anyway)

array("$in" => new MongoId(

This may also need to be an array as a parameter for $in, as in array(new MongoId or you can pass in $series perhaps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank but this doesnt work!!! $ids = $collectionMeasurements->find(array('_id' => array('$in'=>array($doc[measurements]))));
Your $doc["keywords"] (and probably $doc["measurements"] is an array of MongoID. But the constructor of MongoID does not accept an array of values. You need to construct a new MongoID for each of those elements - if they are not already instanceof MongoId
0

You are using

array( $in => new MongoId($doc[keywords]) )

You should use:

array( '$in' => new MongoId($doc[keywords]) )

Use 'single quotes', otherwise with "double quotes" PHP will try to interpret $in as a PHP variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.