Codeigniter MongoDB从选定文档的数组中删除项

Codeigniter MongoDB从选定文档的数组中删除项,codeigniter,mongodb,Codeigniter,Mongodb,我正在使用Codeigniter和Alex Bilbies MongoDB库开发一个web应用程序。 每个用户都得到一个文档,其中包含一个名为phones的项目,该项目包含一个电话号码数组。如何从所选用户文档中从数组中提取项目 感谢所有的帮助 使用MongoDB$pull从数组中删除特定数组项: > db.mycollection.insert({user: "test", items: [1234, 5678, 91011]}); > db.mycollection.find()

我正在使用Codeigniter和Alex Bilbies MongoDB库开发一个web应用程序。 每个用户都得到一个文档,其中包含一个名为phones的项目,该项目包含一个电话号码数组。如何从所选用户文档中从数组中提取项目


感谢所有的帮助

使用MongoDB$pull从数组中删除特定数组项:

> db.mycollection.insert({user: "test", items: [1234, 5678, 91011]});
> db.mycollection.find()
{ "_id" : ObjectId("4ec3b9af8d1ae67f1fb2b30a"), "user" : "test", "items" : [ 1234, 5678, 91011 ] }
> db.mycollection.update({user: "test"}, {$pull: {items: 5678}});
> db.mycollection.find()
{ "_id" : ObjectId("4ec3b9af8d1ae67f1fb2b30a"), "items" : [ 1234, 91011 ], "user" : "test" }

使用MongoDB$pull从阵列中删除特定阵列项:

> db.mycollection.insert({user: "test", items: [1234, 5678, 91011]});
> db.mycollection.find()
{ "_id" : ObjectId("4ec3b9af8d1ae67f1fb2b30a"), "user" : "test", "items" : [ 1234, 5678, 91011 ] }
> db.mycollection.update({user: "test"}, {$pull: {items: 5678}});
> db.mycollection.find()
{ "_id" : ObjectId("4ec3b9af8d1ae67f1fb2b30a"), "items" : [ 1234, 91011 ], "user" : "test" }

甚至我在使用Alex Bilbies MongoDB库直接尝试从数组中提取值时也遇到了问题。 这种方法适用于我,首先取消设置值,然后提取所有空值

希望这有帮助

$this->mongo_db->where('items', "5678")->unset_field('items.$')->update('mycollection');
$this->mongo_db->pull('items', NULL)->update('mycollection');

甚至我在使用Alex Bilbies MongoDB库直接尝试从数组中提取值时也遇到了问题。 这种方法适用于我,首先取消设置值,然后提取所有空值

希望这有帮助

$this->mongo_db->where('items', "5678")->unset_field('items.$')->update('mycollection');
$this->mongo_db->pull('items', NULL)->update('mycollection');

Mongo支持$pull-out-of-the-box:是的,但是我可以拉一个特定的数组项吗?Mongo支持$pull-of-box:是的,但是我可以拉一个特定的数组项吗?很好。但如何从特定文档的数组中提取呢?当使用修饰符操作符时,不需要指定整个文档,只需要指定需要修改的条件和字段。就PHP而言,这看起来像:$coll->update(数组('username'=>'jamesbond',数组('pull'=>array('items'=>5678));好的。但是使用Alex Bilbies库会是什么样子呢?这不起作用:$this->mongo_db->pull('items',$pull)->update('market.carts',$query抱歉,我对这个库一无所知:(我发布的解决方案在mongo shell和PHP driver.Nice中工作。但是如何从特定文档的数组中提取呢?当使用修饰符操作符时,您不需要指定整个文档,只需要指定需要修改的条件和字段。在PHP中,这看起来像:$coll->update(array)('username'=>'jamesbond',array('$pull'=>array('items'=>5678));好的。但是使用Alex Bilbies库会是什么样子?这不起作用:$This->mongo_db->pull('items',$pull)->update('market.carts',$query);很抱歉,我对这个库一无所知:(解决方案我在mongo shell和PHP驱动程序中发布了工作。