简体   繁体   English

如何使用 MongoDB Java 更新文档并设置字段值?

[英]How to update a document and set a field's value using MongoDB Java?

In sql the statement look like: UPDATE table SET table.password = pPassword WHERE (table._id = pid);在 sql 中,语句如下所示: UPDATE table SET table.password = pPassword WHERE (table._id = pid);

How does this work for Java and MongoDB?这对 Java 和 MongoDB 有什么作用?

function(){
BasicDBObject cBsonFilter = new BasicDBObject();
cBsonFilter.append(COL_id, new BasicDBObject("$eq", pid)); // COL_id = _id // pid = is the right id as String

Document cBsonUpdate = new Document();
cBsonUpdate.put(COL_password, pPassword); // COL_password = password // pPassword ist the password_hash as String

// cMongoDatabase = working connection
User.doFindAndUpdateOne(cMongoDatabase, User.class.getSimpleName(), cBsonFilter, cBsonUpdate);
}

public static UpdateResult doFindAndUpdateOne(MongoDatabase cMongoDatabase, String pNameCollection, BasicDBObject pFilter, Document pUpdate) {
    return cMongoDatabase.getCollection(pNameCollection).updateOne(pFilter, pUpdate);
}

Existing:现存的:

{
        "_id" : ObjectId("5ed4b1b45c603146a9abc7d2"),
        "Display" : null,
        "Name" : null,
        "birthdate" : null,
        "image" : null,
        "email" : "example@gmail.com",
        "_lc" : ISODate("2020-06-01T07:44:06.176Z"),
        "lastuserip" : "0:0:0:0:0:0:0:1",
        "userlaw" : 1,
        "validationhash" : null,
        "registered" : ISODate("2020-06-01T07:43:48.843Z"),
        "termsofservice" : null,
        "password" : null
}

To: I want by Id and modify the password. To:我想通过 Id 和修改密码。 I don't want to replace the whole Document.我不想替换整个文档。

{
        "_id" : ObjectId("5ed4b1b45c603146a9abc7d2"),
        "Display" : null,
        "Name" : null,
        "birthdate" : null,
        "image" : null,
        "email" : "example@gmail.com",
        "_lc" : ISODate("2020-06-01T07:44:06.176Z"),
        "lastuserip" : "0:0:0:0:0:0:0:1",
        "userlaw" : 1,
        "validationhash" : null,
        "registered" : ISODate("2020-06-01T07:43:48.843Z"),
        "termsofservice" : null,
        "password" : passwordhash**************************
}

Solution:解决方案:

The Solution is that you nest BasicDBObjects in BasicDBObjects.解决方案是将 BasicDBObjects 嵌套在 BasicDBObjects 中。

new BasicDBObjects("_id", new BasicDBObjects("key", "value"));

There are some Examples:有一些例子:

    ArrayList<BasicDBObject> cFilterObjList = new ArrayList<>();
    cFilterObjList.add(new BasicDBObject(User.COL_id, pUser.getId()));

    BasicDBObject cFilterObj = new BasicDBObject();
    cFilterObj.append(User.COL_Display, new BasicDBObject("$elemMatch", new BasicDBObject(Display.COL_id, this.getId())));

    cFilterObjList.add(cFilterObj);

    BasicDBObject cQueryObj = new BasicDBObject();
    cQueryObj.append(COL_name, this.getName());
    cQueryObj.append(COL_size, this.getSize());
    cQueryObj.append(COL_Address, this.getAddress());

    BasicDBObject cDisplayObj = new BasicDBObject();
    cDisplayObj.append(Display.class.getSimpleName(), cQueryObj);

    BasicDBObject cUpdateObj = new BasicDBObject();
    cUpdateObj.append("$set", cDisplayObj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM