public static void main(String[] args) {
String id = "51a31b1424c6f6929c8194a5";
String mId = "51a31bdb24c6dafc9e6fc618";
DBObject filter = new BasicDBObject();
filter.put("_id", new ObjectId(id));
filter.put("opruser.post.uuid", (mId));
DBObject vals = new BasicDBObject();
vals.put("$inc",
new BasicDBObject("opruser.post.$.ppraise",1));
DBCollection.update(filter, vals);
}
此文中实现该功能的重点在于 opruser.post.$.ppraise,我们在集合和集合元素名中间添加 $ 用来更新符合查询条件的第一个元素。