[英]Push new value to inner array with Lithium and MongoDB
I have a structure like this: 我有这样的结构:
[_id] => MongoId Object (
[$id] => 4e91bcb40b7aab256c000000
)
[campaigns] => Array (
[0] => Array (
[campaign_id] => 4e91bcd10b7aab2b6c000000
[refer_code] => AjZCJR
[owner] => 1
[campaign_name] => Test Campaign
[users] => Array (
)
)
[1] => Array (
[campaign_id] => 4e9210600b7aab276c000000
[refer_code] => FQIMJd
[owner] => 1
[campaign_name] => New Test Campaign
[users] => Array (
)
)
)
I would like to be able to update the campaigns array with new fields and update the existing fields, on the match of the ID. 我希望能够在ID匹配的情况下使用新字段更新广告系列数组并更新现有字段。
Currently I am trying to do the following: 目前,我正在尝试执行以下操作:
$data = array('campaign_name'=>'changed', 'new_field'=>'its new');
User::update(array('$push'=>array('campaigns.$'=>$data)),array('campaigns.campaign_id'=>$campaign_id));
However that is not working - I'm using the positional operator in order to try and match the array to the condition statement, as without it, it just creates a new array within the campaigns array. 但是,这不起作用-我正在使用位置运算符,以尝试将数组与条件语句匹配,因为没有它,它只是在Campaigns数组内创建了一个新数组。
I understand I am probably using it wrong, as I presume it should be used by doing 我了解我可能错误地使用了它,因为我认为应该这样做
'campaigns.$.campaign_name'=>'changed'
However I don't want to be limited to specifying it each time, and was wondering the best way to do this type of update without using a for loop and adding each piece of data (which I'm currently doing as follows:) 但是我不希望每次都指定它,并且想知道在不使用for循环和添加每条数据的情况下进行此类更新的最佳方法(我目前正在执行以下操作:)
$data = array();
foreach($this->request->data as $key=>$value) {
$data['campaigns.$.'.$key] = $value;
}
User::update($data,array('campaigns.campaign_id'=>$campaign_id));
If someone could let me know the 'proper' way to do this, that would be great! 如果有人可以让我知道执行此操作的“正确”方法,那就太好了!
Thanks :) 谢谢 :)
Dan 担
Just update: 只需更新:
$co = array(
'$pull' => array('campaigns.1" => array('campaign_name' => 'changed'' ) )
);
$p->update(array("_id" => new MongoId( $MongoID )), $co);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.