0

这是我的示例文档。

{
    "_id" : ObjectId("51f20148a85e39af87510305"),
    "group_name" : "sai",
    "privileges" : [
        "Notification",
        "News Letter"
    ],
    "users" : [
        {
            "full_name" : "sumit",
            "user_name" : "sumitdesh",
            "password" : "magicmoments",
            "status" : "Active"
        },
        {
            "full_name" : "ad",
            "user_name" : "asd",
            "password" : "asdf",
            "status" : "Active"
        }
    ]
}

我想用新文档替换用户数组中的内部文档。

这是我的java代码:

BasicDBObject g1=new BasicDBObject();
g1.put("full_name", "ram");
g1.put("user_name", "ram123");
g1.put("password", "pass$123");
g1.put("status", "Inactive");
BasicDBObject doc=new BasicDBObject();
doc.put("users",g1);
BasicDBObject q=new BasicDBObject("users.user_name","asd");
con.update(q,doc);

任何帮助表示赞赏

预期输出如下我想用这些值替换内部文档

{
        "_id" : ObjectId("51f20148a85e39af87510305"),
        "group_name" : "sai",
        "privileges" : [
            "Notification",
            "News Letter"
        ],
        "users" : [
            {
                "full_name" : "sumit",
                "user_name" : "sumitdesh",
                "password" : "magicmoments",
                "status" : "Active"
            },
            {
                "full_name" : "ram",
                "user_name" : "ram123",
                "password" : "pass$123",
                "status" : "Inactive"
            }
        ]
    }
4

3 回答 3

1

I must combine $set and $ operators, then you can update an specific item of array.

BasicDBObject g1 = new BasicDBObject();
g1.put("users.$.full_name", "ram");
g1.put("users.$.user_name", "ram123");
g1.put("users.$.password", "pass$123");
g1.put("users.$.status", "Inactive");

BasicDBObject doc = new BasicDBObject();
doc.put("$set", g1);

BasicDBObject q = new BasicDBObject("users.user_name","asd");
con.update(q,doc);
于 2013-07-26T13:33:00.843 回答
0

您的代码将创建一个仅包含新用户的新文档。要将新元素添加到现有文档中的数组,请使用 $push 运算符

BasicDBObject where = new BasicDBObject("_id", new ObjectId("51f20148a85e39af87510305");
BasicDBObject doc = //... your new user object
BasicDBObject push = new BasicDBObject("$push", doc);
con.update(where, push);

要修改现有文档的字段,您可以使用 set-operator 与 $-placeholder 结合使用。这会将用户名从“foo”更改为“bar”

BasicDBObject where =  new BasicDBObject("users.user_name", "foo");
BasicDBObject value =  new BasicDBObject("users.$.user_name", "bar");
BasicDBObject set = new BasicDBObject("$set", value);
con.update(where, set);

但是最不让人头疼的方法是,当您从数据库中检索对象时,只保留整个 DBObject,镜像 DBObject 中的所有修改,然后调用

con.save(dbObject);

An ORM wrapper library can do this for you. But while it is the easiest way, it isn't the most efficient way, because the whole document will be sent to the database. This is a problem which can be easily filed under "premature optimization" when writes are infrequent and the documents are small, but when you save often and have huge documents, it can become an issue.

于 2013-07-26T13:32:48.853 回答
0

This is actually fairly simple, if you follow the documentation.

The first difficulty is finding the document to update. You're looking for the user whose user_name field is 'asd', which is done, rather neatly, through the following query:

{'users.user_name': 'asd'}

The field name needs to be escaped in the mongo shell (it's a compound name) but you need not worry about that in Java.

Now that you've found your user, you need to change it. MongoDB magically stores the matched array index as $, which allows you to write the update criteria as:

{
    $set: {
        'users.$': {
            full_name: 'ram',
            user_name: 'ram123',
            password: 'pass$123',
            status: 'inactive'
        }
    }
}

You obviously know your way around the Java connector, I'll leave the transformation of my JSON object to instances of BasicDBObject to you.

于 2013-07-26T13:44:04.367 回答