0

我有这个结构:

{
 "_id": NumberInt(101),
 "link_id": {
 "125": {
   "thumb_position": NumberInt(1),
   "last_scan": NumberInt(1234563645),
   "row_numb": NumberInt(301),
   "clicks": NumberInt(120)
   },
 "126": {
   "thumb_position": NumberInt(2),
   "last_scan": NumberInt(-2147483648),
   "row_numb": NumberInt(1301),
   "clicks": NumberInt(199)
     },
    {
    ...
    }
}
}   

我想用新的linkids更新文档并得到:

{
"_id": NumberInt(101),
"link_id": {
  "125": {
   "thumb_position": NumberInt(1),
   "last_scan": NumberInt(1234563645),
   "row_numb": NumberInt(301),
   "clicks": NumberInt(120)
},
 "126": {
   "thumb_position": NumberInt(2),
   "last_scan": NumberInt(-2147483648),
   "row_numb": NumberInt(1301),
   "clicks": NumberInt(199)
},
 "127": {
   "thumb_position": NumberInt(1),
   "last_scan": NumberInt(-2147483628),
   "row_numb": NumberInt(1304),
   "clicks": NumberInt(195)
}
}

我在php中尝试过:

$value = array (
'130' => 
array (
  'thumb_position' => 1,
  'last_scan' => 1234563640,
  'row_numb' => 300,
  'clicks' => 120,
));
$update_status = $collection->update( array('_id'=>intval(101)), array('$set' => array('link_id' => $value)) , array("upsert"=>true ,"multiple"=> true , "safe"=> true) );

但这只是用这个 130 覆盖 link_ids。

嵌入式方法......因为这不是一个数组,而是对象,关于如何解决这个问题的任何想法?多谢。

4

1 回答 1

1

请尝试以下代码:

$value = array (
    'thumb_position' => 1,
    'last_scan' => 1234563640,
    'row_numb' => 300,
    'clicks' => 120,
);

$update_status = $collection->update(
    array('_id'=>intval(101)),
    array('$set' => array('link_id.130' => $value)),
    array("upsert"=>true ,"multiple"=> true , "safe"=> true)
);
于 2013-03-04T19:49:03.940 回答