0
votes

Now I have a "answers" array and I want to remove the first of this array but index item of this array is 1 but not 0. I don't know mongodb has support remove item array and update index array ? My code:

Before remove:

array (
'_id' => new MongoId("52818f9ad069feed198b4567"),
'answers' => array (

'0' => 
       array (
              'by' => new MongoId("52818ec2d069fe90058b4567"),
              'content' => '<p>Ok, testing<br /> &nbsp;</p> ',
              'date' => new MongoInt32(1384222656),
             ),
'1' => 
             array (
               'by' => new MongoId("528192acd21b6310188b4567"),
               'content' => 'test',
               'date' => new MongoInt32(1384224360),
              ),   

),

After remove the first item of answers array

array (
'_id' => new MongoId("52818f9ad069feed198b4567"),
'answers' => array (

'1' => 
             array (
               'by' => new MongoId("528192acd21b6310188b4567"),
               'content' => 'test',
               'date' => new MongoInt32(1384224360),
              ),   

),

My result needs

array (
'_id' => new MongoId("52818f9ad069feed198b4567"),
'answers' => array (

'0' => 
             array (
               'by' => new MongoId("528192acd21b6310188b4567"),
               'content' => 'test',
               'date' => new MongoInt32(1384224360),
              ),   

),

My English very bad but please help me !

1
What code are you using to remove the first entry? As 0 is normally the first array key, you actually want to remove the second? - scrowler
Above code is an example data that I store in mongodb. I want: 1. Remove the first entry 2. Update index of the second entry to 0. Have function in mongodb can do that ? - Đầu Trọc
Array_values will reset keys - scrowler

1 Answers

0
votes

use $db.collection.update( {array('_id' => new MongoId("52818f9ad069feed198b4567")), array( $unset=> array( '0'=> true ) ) ) use $db.collection.update( {array(), array( $unset=> array( '0'=> true ) ) )

to make the same modification for every documents.