2013-05-18 2 views
0

PHP로 MongoDB를 배우려고합니다. -> save ($ object)를 사용하여 새 문서를 성공적으로 저장할 수 있습니다. 내 목표는 특정 _id 가진 문서에 하위 문서를 추가하는 것입니다. 나는 "이동"으로 새 문서를 삽입 할PHP/MongoDB - _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']; 
    } 
} 

내가 읽은 내용을 바탕으로,이 안에 중첩 된 문서를 만들어 페이지를 다시로드 할 때마다 5197f0cef045a1d710000032.moves한다.

내가 무엇을 놓치고 있는지 잘 모르겠습니다.

감사합니다, 나중에 같은 분야에 $push를 사용할 계획이라면 당신은 간단한 값으로 설정 $를 사용하지 않아야

미첼

답변

2

.

처음으로 업데이트 할 때 $push을 사용하십시오. 단일 요소의 배열을 만든 다음 더 많은 요소를 추가 (밀어 넣기) 할 수 있습니다.

+0

감사합니다. 왜 숲에있는 나무를 찾을 수 없었는지 잘 모르겠습니다. :) – xandout

관련 문제