2016-04-26 17 views
1

Erste Abfrage funktioniert, wenn ich bei Robomongo laufe. Allerdings habe ich Probleme, es in PHP zu konvertieren.MongoDB-Abfrage in PHP konvertieren

Ich habe die aktualisierte Version, bitte lassen Sie mich wissen, wenn ich etwas vermisse.

Dies funktioniert (MongoDB Abfrage)

db.statusNew.find(
    {_id: ObjectId("123")}, 
    { 
     statuses: { 
     $elemMatch: { 
      id : 321 
     }} 
    } 
).limit(1) 

Das funktioniert nicht (übersetzt PHP)

$queryOri = array(
    (_id: new MongoId($id)), 
    (
     statuses: (
     $elemMatch: (
      id : $tweetID['id'] 
    )) 
) 
); 

$query = $collection.find($queryOri).limit(1); 

aktualisiert!

$collection -> find(
    array('_id' => new MongoId($id)), 
    array(
     'statuses' => array(
      $elemMatch: (
       id: $tweetID['id'] 
      ) 
     ) 
    ) 
); 

Array sieht wie folgt aus:

Array 
(
    [_id] => MongoId Object 
    (
     [$id] => 123 
    ) 

    [statuses] => Array 
    (
     [0] => Array 
      (
      [id] => 321 
      [text] => Tweet no 1 
      ) 
     [1] => Array 
      (
      [id] => 322 
      [text] => Tweet no 2 
      ) 
     [2] => Array 
      (
      [id] => 323 
      [text] => Tweet no 3 
      ) 
    ) 
) 

Antwort

0
$collection->find(
    array('_id' => new MongoId($id)), 
    array(
     'statuses' => array(
      '$elemMatch' => array(
       'id'=>$tweetID['id'] 
      ) 
     ) 
    ) 
); 

Sie haben Doppelpunkte zu verwenden (:) statt assingment (=>). In mongodb werden alle geschweiften Klammern in PHP nur als Array betrachtet. Hoffe, du hast diese Antwort.