2013-04-18 62 views
0

的值我有以下形式的文件:的MongoDB + PHP:删除子阵列

[uuid] => d030b8d1 
[commentstrings] => Array (
    [0] => 1366220389#[email protected]#test 1 
    [1] => 1366220422#[email protected]#test 2 
    [2] => 1366220458#[email protected]#test 3 
) 

我有commentstrings之一的满弦和要删除该值。

如果我试试这CLI,它的工作原理:

db.messages.update(
    {'uuid':'d030b8d1'}, 
    { $pull : { 
     'commentstrings': '1366220422#[email protected]#test 2' 
    }} 
) 

但是,如果我尝试在PHP没有同样的情况:

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

任何想法,我在做什么错在这里?

回答

1

我没有得到这个行为:

$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()); 

打印:

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) 

什么版本的驱动程序你,也是你的PHP版本是?您是否确定commentstrings是数组?查看MongoDB控制台,而不是通过PHP,看看它打印出来的样子。

+0

感谢您的测试用例 - 它也在这里工作。 我发现问题 - 这是一个错字! : -/ 感谢您的帮助! – blitzkid

+0

@blitzkid heh我们都这么做:)很高兴有帮助 – Sammaye

0

试试这个

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

我不能使用mongo id。我已经找到了它自己的uuid上的项目。问题是删除其中一个评论串。 – blitzkid

+0

也许问题出在'1366220422#[email protected]#test 2'把'1366220422 \ #mac \@test.org \ #test 2'代替,然后再试 –

+0

或者问题出在mongodb上。复位mongodb的并再次尝试 –

0

尝试

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

什么回报?

+0

'[updatedExisting] => [N] => 0 [的ConnectionId] => 2314 [ERR] => [OK] => 1' 似乎确定,但不删除任何东西。 – blitzkid

0

原来我的代码是正确的。这是一个变量的错字。