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
Codeigniter 使用MongoDB从阵列中删除特定项_Codeigniter_Mongodb - Fatal编程技术网

Codeigniter 使用MongoDB从阵列中删除特定项

Codeigniter 使用MongoDB从阵列中删除特定项,codeigniter,mongodb,Codeigniter,Mongodb,我正在使用Codeigniter和MongoDB开发一个web应用程序。 用户可以上传文件,其他用户可以对其进行评论 我将注释存储在主文件文档中名为comments的数组中。 这很好,但是如何从数组中删除特定注释 我不能使用ID作为键,因为用户可以添加多个注释。你会怎么做 你建议我能做吗 这是我的注释数组: "comments": [ { "user_id": ObjectId("4f240b433dc7937d68030000"),

我正在使用Codeigniter和MongoDB开发一个web应用程序。 用户可以上传文件,其他用户可以对其进行评论

我将注释存储在主文件文档中名为comments的数组中。 这很好,但是如何从数组中删除特定注释

我不能使用ID作为键,因为用户可以添加多个注释。你会怎么做 你建议我能做吗

这是我的注释数组:

"comments": [
        {
            "user_id": ObjectId("4f240b433dc7937d68030000"),
            "user_name": "james",
            "user_comment": "This is a comment",
            "created_at": "2012-01-2821: 20: 44"
        },
        {
            "user_id": ObjectId("4f240b433dc7937d68030000"),
            "user_name": "mandy",
            "user_comment": "This is another comment",
            "created_at": "2012-01-2821: 31: 07"
        }
    ],

如果可以通过匹配userid、name或comment来识别注释项,那么可以使用带有
$pull
修饰符的
update()
命令以及适当的条件删除该注释

如果无法执行上述操作,请在注释中包含唯一id(如
UUID

要删除注释,请执行以下操作:

db.coll.update({<cond to identify document}, {$pull: {'comments': {'name': <name>}}} )

db.coll.update({也许您可以通过在
时间创建的“
删除注释,因为时间是唯一的

$db.coll.update({cond to identify document},{$pull:{'comments':{'created_at':<>}}})
$db.coll.update({cond to identification document},{$pull:{'comments':{'created_at':}})

我认为,在插入每个注释时,您必须向其添加mongodb id。您可以像这样创建一个新的mongodb id

$comment_id = new MongoID();

现在,您可以使用$pull+$update按id删除注释,就像@smoorthy的答案一样。

听起来很有趣。我可以让mongoDB分配UUID吗?如果是,如何分配?否。对于文档中的值,如本例,您需要生成UUID。PHP有uniqid(),您也可以使用它。感谢您提供了全面的答案。这篇文章中的文档比较平淡无奇。但是我如何才能为数组对象中的字段添加索引?您能否演示我们如何使用Linq for Mongo DB c#实现这一点?不过,多个用户可以在同一秒内发布,因此我会为每个评论生成一个唯一的id。
$comment_id = new MongoID();