1

コレクションにはこのようなデータが含まれています

    {
        '_id': ObjectId("527cf8ae3ad5a461caf925fc"),
        'name': {
            'first': 'John',
            'last': 'Backus'
        }
    }

$where で「John」という名のレコードを検索したい

    $m = new MongoClient();
    $db = $m->selectDB('school');
    $collection = new MongoCollection($db, 'student');
    $js = "function() {
        return this.first.name == 'John';
    }";
    $cursor = $collection->find(array('$where' => $js));

例外が発生しています Caught exception: localhost:27017: TypeError: Cannot read property 'first' of undefined near 'this.name.first=='Jo'

$whereだけで検索したい。

4

2 に答える 2

0

これは私のために働いた:

   $m = new MongoClient();
   $db = $m->school;
   $collection = $db->student;
   $cursor = $collection->find( array('name.first' => 'John' ));

   foreach ($cursor as $document) {
      var_dump($document);
   }
于 2016-06-14T12:37:31.383 に答える