PHP + MongoDB试图更新|不能使用part(..)遍历元素

时间:2016-04-22 14:52:18

标签: php mongodb

我正在尝试更新存储在mongo中的数组,但我收到此错误cannot use the part (data of data.info) to traverse the element。我已经尝试了很多东西并阅读了很多其他帖子,但没有一个适合我的问题,并且没有足够的文档来说明如何使用新驱动程序使用PHP。

我需要在名为info的文档数组中添加一个文档。

这就是我在PHP中所做的:

$conn = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$options = ["upsert" => true];

$document = ['$push' => ["data.info" => ["user" => "Megan", "hours" => [12, 13]]]];

$filtro = ["year" => 2016, "month" => 5, "day" => 18, "data.room" => 1];

$bulk->update($filtro, $document, $options);
$aux = $conn->executeBulkWrite("prueba.books3", $bulk);

我存储的数据:

{  
   "_id":ObjectId("571a0eb07d67f0b82324e683"),
   "year":2016,
   "month":5,
   "day":18,
   "data":[  
      {  
         "room":1,
         "info":[  
            {  
               "user":"Jack",
               "hours":[  
                  10,
                  11,
                  16
               ]
            }
         ]
      }
   ]
}

更新后应该如何看待的示例:

{  
   "_id":ObjectId("571a0eb07d67f0b82324e683"),
   "year":2016,
   "month":5,
   "day":18,
   "data":[  
      {  
         "room":1,
         "info":[  
            {  
               "user":"Jack",
               "hours":[  
                  10,
                  11,
                  16
               ]
            },
            {  
               "user":"Megan",
               "hours":[  
                  12,
                  13
               ]
            }
         ]
      }
   ]
}

0 个答案:

没有答案