考虑有一个已经加载 googleId 的用户(通过了 google auth),我想使用 Jongo 将它插入到 mongoDB:
// Init
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB(DB_NAME);
Jongo jongo = new Jongo(db);
org.jongo.MongoCollection collectionJongo = jongo.getCollection(USER_COLLECTION_NAME);
// Upsert
String query = "{googleId: {$eq: #}}";
collectionJongo.update(query, user.googleId)
.upsert()
.with(user);
user = collectionJongo.find(query, user.googleId).as(User.class).next();
// Works only on insert. On update, the upsertedId is null.
//user._id = ((ObjectId) writeResult.getUpsertedId()).toHexString();