如何使用php取消mongodb中的所有记录字符串? [英] How to unset all records string in mongodb using php?
问题描述
我的数据库记录了这样的东西...
My DB records somthing like this...
{
"_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";使用下面的代码从所有记录中删除字段,但它不起作用.
I try to use $unset "dbid" from all records like delete field by using the code below but it doesn't works.
PS:我尝试在 stackoverflow 中搜索,但总是在命令行中显示教程,但我需要它在 PHP 脚本中工作.
PS: I try to search in stackoverflow but always show tutorial in commmand line but I need it works in PHP script.
$COLLECTION->update(array('$unset' => array('dbid' => true)));
我试过这段代码也没有用.:(
I try this code didn't work too. :(
$COLLECTION->update(array('$unset' => array('dbid' => true)), array('multiple'=>true));
如果你们有任何想法,请分享..
If you guys have any ideas please share..
非常感谢.:)
--------- 更新 ----------
--------- Updated ----------
我已经找到了问题的解决方案.
I have already found the solution of the problem.
@马克你是对的!我忘记了标准我发现我的代码也是错误的.
@Marc You're right! I forgot the criteria and I found my code is wrong too.
问题是 mongodb 扩展与定义值混淆.(它认为 DB_DBNAME 是一个数据库名称)所以,我添加+更改了一些代码看起来像这样.
The problem is mongodb extension is confused with the define value. (It think DB_DBNAME is a database name) so, I added+changed some code look like this.
$mongodb_name = DB_DBNAME;
$MONGODB = $DB->$mongodb_name;
$COLLECTION = $MONGODB->my_customer;
现在,我尝试@Marc 示例代码
Now, I try @Marc example code
从所有记录中删除 ($unset) 字符串...(有效!)
To remove ($unset) string from all records... (Works!)
$COLLECTION->update(array('dbid' => array('$exists' => true)), array('$unset' => array('dbid' => true)), array('multiple' => true));
要将字符串插入所有记录...我使用此代码.(有效!)
To insert string into all records... I use this code. (Works!)
$COLLECTION->update(array('dbid' => array('$exists' => false)), array('$set' => array('dbid' => 'new_value')), array('multiple' => true));
推荐答案
你忘记了标准,所以解决方案是:
You forgot the critearia, so the solution would be:
$COLLECTION->update(
array('dbid' => array('$exists' => true)),
array('$unset' => array('dbid' => true)),
array('multiple' => true)
)
这篇关于如何使用php取消mongodb中的所有记录字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!