Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB+;PHP:删除子数组的值_Php_Mongodb_Pull - Fatal编程技术网

MongoDB+;PHP:删除子数组的值

MongoDB+;PHP:删除子数组的值,php,mongodb,pull,Php,Mongodb,Pull,我有以下格式的文件: [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 ) 我有一个完整的commentstring字符串,希望删除该值 如果我在CLI上尝试此操作,它可以

我有以下格式的文件:

[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
)
我有一个完整的commentstring字符串,希望删除该值

如果我在CLI上尝试此操作,它可以工作:

 db.messages.update(
     {'uuid':'d030b8d1'}, 
     { $pull : {
         'commentstrings': '1366220422#mac@test.org#test 2'
     }} 
 )
但是,如果我在PHP中尝试相同的方法,则什么也不会发生:

$response = $stdb->messages->update(
    array('uuid'=>'d030b8d1'),
    array('$pull' => array('commentstrings' => '1366220422#mac@test.org#test 2'))
);
知道我做错了什么吗?

试试这个

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


什么回报?

我不明白这种行为:

$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());
印刷品:

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)
您使用的是哪个版本的驱动程序,以及您的PHP版本


您还确定
commentstrings
是一个数组吗?查看MongoDB控制台,而不是通过PHP,看看它是如何打印出来的。

结果证明我的代码是正确的。这是一个变量的输入错误

`[UpdateExisting]=>[n]=>0[connectionId]=>2314[err]=>[ok]=>1'似乎可以,但没有删除任何内容。我无法使用mongo id。我已经在它自己的uuid上找到了该项。问题是删除一个CommentString。问题可能出在
1366220422上#mac@test.org#测试2
put
1366220422\#mac\@test.org\#测试2
,然后再试一次,或者问题可能出在mongodb上。重置mongodb并重新尝试测试用例的Hanks-它也在这里工作。我发现了问题-这是一个输入错误和一个变量!-/谢谢你的帮助@布莱兹小子,呵呵,我们都这么做了:)很高兴能帮上忙
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)