0

I have this structure:

{
 "_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)
     },
    {
    ...
    }
}
}   

and I want to update the document with new linkids and get:

{
"_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)
}
}

I have tried in 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) );

but this is only overwriting the link_ids with this 130.

the embedded aproach... since this is not an array, but objects, any idea on how to solve this? Thx a lot.

5
  • What happens if you use array('$set' => array('link_id.130' => $value['130'])) instead? Commented Mar 4, 2013 at 18:40
  • thx @nutlike for your answer. I try this and the result was: code { "_id": NumberInt(100), "link_id": { "129": { "thumb_position": NumberInt(1), "last_scan": NumberInt(1234563649), "row_numb": NumberInt(309), "clicks": NumberInt(129) }, "130": null } } the value was: code $value_i = array ( 'thumb_position' => 1, 'last_scan' => 1234563641, 'row_numb' => 301, 'clicks' => 121, ); so it added a null entrance. Guess the way is around. Thx again. Commented Mar 4, 2013 at 19:17
  • its now working.. just like u said... not using the 'code' $value_i = array ( 'thumb_position' => 1, 'last_scan' => 1234563641, 'row_numb' => 301, 'clicks' => 121, ); but the 'code' $value['130'].. dont get the difference... thx a lot! Commented Mar 4, 2013 at 19:52
  • to delete one of those embedded entries would be: code $update_status = $collection->update( array('_id'=>intval(100), 'link_id'=> "133"), array('$set' => array('link_id.133' => null)) , array("upsert"=>true ,"multiple"=> true , "safe"=> true) ); ? Commented Mar 4, 2013 at 19:59
  • You should use $unset in this case. Commented Mar 4, 2013 at 20:03

1 Answer 1

1

Try following code instead:

$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)
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.