PHP/MongoDB-将子文档添加到具有_id的文档中

PHP/MongoDB-将子文档添加到具有_id的文档中,php,mongodb,Php,Mongodb,我正在尝试用PHP学习MongoDB。我可以使用->保存($object)成功保存新文档。我的目标是将子文档添加到具有特定id的文档中 我有一份文件: "5197f4bef045a1d710000032": { "_id": { "$id": "5197f4bef045a1d710000032" }, "tag": 5487, "serial": "1z5589ss56", "type": "Soda Can", "dept": "NOC", "location": "Mitchell",

我正在尝试用PHP学习MongoDB。我可以使用->保存($object)成功保存新文档。我的目标是将子文档添加到具有特定id的文档中

我有一份文件:

"5197f4bef045a1d710000032": {
"_id": {
  "$id": "5197f4bef045a1d710000032"
},
"tag": 5487,
"serial": "1z5589ss56",
"type": "Soda Can",
"dept": "NOC",
"location": "Mitchell",
"date": 1368913086,
"moves": null
}
我想在“移动”中插入一个新文档

我曾经能够使用$set进行一次操作,但是随后的$push没有任何作用

    <?php


$m = new Mongo();

$d = $m->selectDB('peeps');
$c = $d->family;
$fields = array('tag' => 5487 , 'serial' => '1z5589ss56', 'type' => 'Soda Can', 'dept' => 'NOC', 'location' => 'Mitchell', 'date' => time() );

$can = new Asset($fields);
#$c->save($can);

#Update to insert move

$c->update(array('_id' => new MongoID('5197f0cef045a1d710000032')), array('$push' => array('moves' => $can)));


$cur = $c->find();
$J = json_encode(iterator_to_array($cur));
print($J);


class Asset{

    public $tag;
    public $serial;
    public $type;
    public $dept;
    public $location;
    public $date;
    public $moves;

    public function __construct($fields){
        $this->tag = $fields['tag'];
        $this->serial = $fields['serial'];
        $this->type = $fields['type'];
        $this->dept = $fields['dept'];
        $this->location = $fields['location'];
        $this->date = $fields['date'];
    }
}

如果您计划以后使用
$push
到同一字段,则不应使用带有简单值的$set


第一次更新时也使用
$push
。它将创建一个由单个元素组成的数组,然后您可以向其中添加(推送)更多元素。

谢谢您,我不知道为什么我无法在林中找到树。:)