0

I have a document in the following form:

[uuid] => d030b8d1
[commentstrings] => Array (
    [0] => 1366220389#[email protected]#test 1
    [1] => 1366220422#[email protected]#test 2
    [2] => 1366220458#[email protected]#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#[email protected]#test 2'
     }} 
 )

But if I try the same in PHP nothing happens:

$response = $stdb->messages->update(
    array('uuid'=>'d030b8d1'),
    array('$pull' => array('commentstrings' => '1366220422#[email protected]#test 2'))
);

Any idea, what I'm doing wrong here?

4 Answers 4

1

I do not get this behaviour:

$mongodb->ghghghg->insert(array('uuid' => 'd030b8d1',
        'commentstrings' => Array (
            '1366220389#[email protected]#test 1',
            '1366220422#[email protected]#test 2',
            '1366220458#[email protected]#test 3'
)));

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

$response = $mongodb->ghghghg->update(
        array('uuid'=>'d030b8d1'),
        array('$pull' => array('commentstrings' => '1366220422#[email protected]#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#[email protected]#test 1' (length=30)
      1 => string '1366220422#[email protected]#test 2' (length=30)
      2 => string '1366220458#[email protected]#test 3' (length=30)

array
  '_id' => 
    object(MongoId)[18]
      public '$id' => string '516ffff96803fa2261000000' (length=24)
  'commentstrings' => 
    array
      0 => string '1366220389#[email protected]#test 1' (length=30)
      1 => string '1366220458#[email protected]#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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your testcase - it is working here too. I found the problem - it was a typo an a variable! :-/ Thanks for helping!
@blitzkid heh we all do it :) glad to have helped
0

Try

$data = array('$pull' => array('commentstrings' => '1366220422#[email protected]#test 2'));
$response = $stdb->messages->update(array('uuid'=>'d030b8d1'), $data);
var_dump($response);

What returns ?

1 Comment

`[updatedExisting] => [n] => 0 [connectionId] => 2314 [err] => [ok] => 1' Seems ok, but does not delete anything.
0

try this

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

3 Comments

I can't use the mongo id. I already found the item over it's own uuid. Problem is to delete one of the commentstrings.
maybe problem is on 1366220422#[email protected]#test 2 put 1366220422\#mac\@test.org\#test 2 instead and try again
or maybe problem is on mongodb. reset mongodb and try again
0

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

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.