Search code examples
javamongodbmongodb-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);

How does this work for Java and 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" : "[email protected]",
        "_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. I don't want to replace the whole Document.

{
        "_id" : ObjectId("5ed4b1b45c603146a9abc7d2"),
        "Display" : null,
        "Name" : null,
        "birthdate" : null,
        "image" : null,
        "email" : "[email protected]",
        "_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

  • Solution:

    The Solution is that you nest BasicDBObjects in 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);