0

I have a document in the following form:

[uuid] => d030b8d1
[commentstrings] => Array (
    [0] => 1366220389#mac@test.org#test 1
    [1] => 1366220422#mac@test.org#test 2
    [2] => 1366220458#mac@test.org#test 3
)

I have a full string of one of the commentstrings and want to delete that value.

If I try this on CLI, it works:

 db.messages.update(
     {'uuid':'d030b8d1'}, 
     { $pull : {
         'commentstrings': '1366220422#mac@test.org#test 2'
     }} 
 )

But if I try the same in PHP nothing happens:

$response = $stdb->messages->update(
    array('uuid'=>'d030b8d1'),
    array('$pull' => array('commentstrings' => '1366220422#mac@test.org#test 2'))
);

Any idea, what I'm doing wrong here?

Sammaye
  • 43,242
  • 7
  • 104
  • 146
psic4t
  • 88
  • 2
  • 6

4 Answers4

1

I do not get this behaviour:

$mongodb->ghghghg->insert(array('uuid' => 'd030b8d1',
        'commentstrings' => Array (
            '1366220389#mac@test.org#test 1',
            '1366220422#mac@test.org#test 2',
            '1366220458#mac@test.org#test 3'
)));

var_dump($mongodb->ghghghg->findOne());

$response = $mongodb->ghghghg->update(
        array('uuid'=>'d030b8d1'),
        array('$pull' => array('commentstrings' => '1366220422#mac@test.org#test 2'))
);

var_dump($mongodb->ghghghg->findOne());

Prints:

array
  '_id' => 
    object(MongoId)[19]
      public '$id' => string '516ffff96803fa2261000000' (length=24)
  'uuid' => string 'd030b8d1' (length=8)
  'commentstrings' => 
    array
      0 => string '1366220389#mac@test.org#test 1' (length=30)
      1 => string '1366220422#mac@test.org#test 2' (length=30)
      2 => string '1366220458#mac@test.org#test 3' (length=30)

array
  '_id' => 
    object(MongoId)[18]
      public '$id' => string '516ffff96803fa2261000000' (length=24)
  'commentstrings' => 
    array
      0 => string '1366220389#mac@test.org#test 1' (length=30)
      1 => string '1366220458#mac@test.org#test 3' (length=30)
  'uuid' => string 'd030b8d1' (length=8)

What version of the driver are you on and also your PHP version?

Also are you sure that commentstrings IS an array? Look in the MongoDB console, not through PHP and see what it print it out like.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
0

try this

$response = $stdb->messages->update(array('uuid'=>'d030b8d1'),array('$pull' => array('commentstrings' => '1366220422\#mac\@test.org\#test 2')));
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
0

Try

$data = array('$pull' => array('commentstrings' => '1366220422#mac@test.org#test 2'));
$response = $stdb->messages->update(array('uuid'=>'d030b8d1'), $data);
var_dump($response);

What returns ?

diegoprates
  • 949
  • 6
  • 9
  • `[updatedExisting] => [n] => 0 [connectionId] => 2314 [err] => [ok] => 1' Seems ok, but does not delete anything. – psic4t Apr 18 '13 at 14:01
0

Turns out that my code was correct. It was a typo in a variable.

psic4t
  • 88
  • 2
  • 6