1. 程式人生 > >MongoDB在插入後,如何獲取對應的ObjectId

MongoDB在插入後,如何獲取對應的ObjectId

DBObject val = new DBObject();

val.put("userID", 10002);

val.put("userName", "張三");

//dbColl為collection

dbColl.save(val);

ObjectId _id = (ObjectId)val.get("_id");//這是因為傳入的val是一個物件,在save過程中,引擎對DBObject賦值,所以能夠取到值