相关疑难解决方法(0)

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

我正在使用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"
        }
    ],
Run Code Online (Sandbox Code Playgroud)

codeigniter mongodb

35
推荐指数
1
解决办法
4万
查看次数

MongoDB $ pull查询不会删除和更新现有记录

我主要担心的是在mongodb文档中他们使用$elemMatch命令进行查找并从数组中提取元素,但是当我使用时这不起作用.我的文档结构是

{
"_id" : ObjectId("55dea445c3ad8cac03a7ed8e"),
"email" : "james@user.com",
"groups" : [ 
    {
        "_id" : ObjectId("55dea4a4c3ad8c8005a7ed8f"),
        "name" : "All Group"
    }
]}
Run Code Online (Sandbox Code Playgroud)

我想使用mongodb查询从文档中删除组.我的查询是:

db.users.update({"_id": ObjectId('55dea445c3ad8cac03a7ed8e')}, 
{$pull: {"groups": {$elemMatch: {"_id": ObjectId('55dea4a4c3ad8c8005a7ed8f')}}}})
Run Code Online (Sandbox Code Playgroud)

执行查询后,用户文档未更新,组值仍然存在.我关注mongodb文档:http://docs.mongodb.org/v2.6/reference/operator/update/pull/

mongodb mongodb-query mongodb-update

4
推荐指数
1
解决办法
2821
查看次数