如何使用 MongoDB Java 更新文档并设置字段值?
How to update a document and set a field's value using MongoDB Java?
在 sql 中,语句如下所示: UPDATE table SET table.password = pPassword WHERE (table._id = pid);
这对 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);
}
现有:
{
"_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
}
致:我想通过Id修改密码。我不想替换整个文档。
{
"_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**************************
}
解决方案:
解决方案是将 BasicDBObjects 嵌套在 BasicDBObjects 中。
new BasicDBObjects("_id", new BasicDBObjects("key", "value"));
有一些例子:
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);
在 sql 中,语句如下所示: UPDATE table SET table.password = pPassword WHERE (table._id = pid);
这对 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);
}
现有:
{
"_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
}
致:我想通过Id修改密码。我不想替换整个文档。
{
"_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**************************
}
解决方案:
解决方案是将 BasicDBObjects 嵌套在 BasicDBObjects 中。
new BasicDBObjects("_id", new BasicDBObjects("key", "value"));
有一些例子:
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);