如何使用php在mongodb中取消设置所有记录字符串


How to unset all records string in mongodb using php?

我的数据库记录了这样的事情。。。

{
   "_id": ObjectId("50118b145e69ef2c0e007a2"),
   "class": "customer",
   "dbid": "1829",
   "value": "aaa@hotmail.com" 
}
{
   "_id": ObjectId("50118b145e69ef2c0e007a1"),
   "class": "customer",
   "dbid": "1828",
   "value": "bbb@hotmail.com" 
}

我的PHP代码

define('DB_HOST', 'localhost');
define('DB_DBNAME', 'mydb');
define('DB_USERNAME', 'mongouser');
define('DB_PASSWORD', 'mongopasswd');
define('DB_DSN', "mongodb://". DB_USERNAME .":". DB_PASSWORD ."@". DB_HOST ."/". DB_DBNAME);
$DB = new Mongo(DB_DSN);
if (!$DB) { die('DB Connection failed!'); }
$MONGODB = $DB->DB_DBNAME;
$COLLECTION = $MONGODB->my_customer;

我尝试使用$unset";dbid";使用下面的代码从所有记录中删除字段,但它不起作用。

PS:我尝试在stackoverflow中搜索,但总是在命令行中显示教程,但我需要它在PHP脚本中工作。

$COLLECTION->update(array('$unset' => array('dbid' => true)));

我尝试过这个代码也不起作用。:(

$COLLECTION->update(array('$unset' => array('dbid' => true)), array('multiple'=>true));

如果你们有什么想法,请分享。。

非常感谢。:)


---------更新----------------

我已经找到了这个问题的解决办法。

@Marc你说得对!我忘记了标准我发现我的代码也错了。

问题是mongodb扩展与define值混淆了。(它认为DB_DBNAME是一个数据库名称)所以,我添加了+更改了一些代码,如下所示。

$mongodb_name = DB_DBNAME;
$MONGODB = $DB->$mongodb_name;
$COLLECTION = $MONGODB->my_customer;

现在,我尝试@Marc示例代码

要从所有记录中删除($unset)字符串。。。(有效!)

$COLLECTION->update(array('dbid' => array('$exists' => true)), array('$unset' => array('dbid' => true)), array('multiple' => true));

要在所有记录中插入字符串。。。我使用这个代码。(有效!)

$COLLECTION->update(array('dbid' => array('$exists' => false)), array('$set' => array('dbid' => 'new_value')), array('multiple' => true));

您忘记了标准,所以解决方案是:

$COLLECTION->update(
    array('dbid' => array('$exists' => true)),
    array('$unset' => array('dbid' => true)),
    array('multiple' => true)
)

我认为这样的东西会在中工作

    $collection->update(
        array( '$unset' => array('dbid' => true) );
        array("upsert" => true, "multiple" => true)
    );