我将以下对象存储在MongoDB中:
[_id] => MongoId Object (
)
[horses] => Array (
[0] => Array (
[name] => Youve Been Mowed
[selectionId] => 3004097
)
[1] => Array (
[name] => Rascal In The Mix
[selectionId] => 3460932
)
[2] => Array (
[name] => Clumber Place
[selectionId] => 2986884
)
[3] => Array (
[name] => Pretty Orchid
[selectionId] => 2581717
)
[4] => Array (
[name] => Astrodonna
[selectionId] => 2568095
)
[5] => Array (
[name] => Just Sam
[selectionId] => 2511403
)
[6] => Array (
[name] => Many Welcomes
[selectionId] => 2866423
)
[7] => Array (
[name] => Two Turtle Doves
[selectionId] => 3857873
)
[8] => Array (
[name] => Piquante
[selectionId] => 3372813
)
[9] => Array (
[name] => Nimmys Special
[selectionId] => 3066557
)
)
[marketId] => 101531031
[marketName] => 7f Hcap
现在我要添加一个新字段:
[_id] => MongoId Object (
)
[horses] => Array (
[0] => Array (
[name] => Youve Been Mowed
[selectionId] => 3004097
[odds] => Array(
10000000 => 1.5
10000020 => 1.6
10000030 => 1.7
)
)
...
etc.
...
100000XX是时间戳,1.X是赔率
我真的在桌子上敲我的头。任何建议都非常赞赏。
这就是我所说的(错误的):
foreach($horses as &$horse)
{
$newdata=array('$set'=>array($horse['odds']=>$price));
$filter=array("marketId"=>$marketId);
$c->update($filter,$newdata);
}
答案 0 :(得分:0)
您需要使用点表示法:
foreach($horses as &$horse)
{
$newdata=array('$set'=>array('horses.odds'=> array($horses['odds'] => $price)));
$filter=array("marketId"=> $marketId);
$c->update($filter,$newdata);
}
请参阅http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29。
答案 1 :(得分:0)
非常感谢您的回复。你是CPAN上MongoDB Perl模块的Kristina吗? :)
这是我的工作代码:
$data_object=null;
for($i=0;$i<sizeof($horses);$i++)
{
$data_object->timestamp=$time;
$data_object->niceTime=date("c");
$data_object->price=$price;
$this_horse=$horses[$i];
if($this_horse['selectionId']==$horseId)
{
$newdata=array('$push'=>array("horses.$i.odds"=>$data_object));
$filter=array();
$options=array('upsert'=>true);
$c->update($filter,$newdata,$options);
}
}