我正在使用Java来操作mongodb,我有一个用户对象,并且在每个对象中都有一个密码对象的数组属性。
public static void update(){
MongoClient mongoClient = Mongo.getInstance().conectionMongo();
MongoDatabase database = mongoClient.getDatabase("mesh_sso");
MongoCollection<Document> userCollection = database.getCollection("user");
String userId = "5d3e9d3d61834e0288c36906";
String password = "333";
try {
BasicDBObject searchQuery = new BasicDBObject().append("password",password);
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$set",new BasicDBObject("status",1));
// test
Document filter = new Document("recordpassword", new Document().append("$elemMatch", new Document().append("status", 0 )));
userCollection.updateOne(filter,updateQuery);
}catch (Exception ex) {
ex.printStackTrace();
}
}
“我不知道哪里出了问题,我想更新recordpassword的一个元素:当密码为'333'时,状态更改为1,如果有人知道,请帮助我,我感谢!”