From a57ad8ce6d399e48ee6d22d329c4068f24e105ff Mon Sep 17 00:00:00 2001 From: Benson Peng Date: Sun, 7 Oct 2012 11:52:53 +0800 Subject: [PATCH] add findAndModifyUpsert --- src/com/orange/common/mongodb/MongoDBClient.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/com/orange/common/mongodb/MongoDBClient.java b/src/com/orange/common/mongodb/MongoDBClient.java index 725c416..3858018 100644 --- a/src/com/orange/common/mongodb/MongoDBClient.java +++ b/src/com/orange/common/mongodb/MongoDBClient.java @@ -171,6 +171,20 @@ public DBObject findAndModify(String tableName, DBObject query, false); } + // returnNew = true, upsert = true + public DBObject findAndModifyUpsert(String tableName, DBObject query, + DBObject update) { + DBCollection collection = db.getCollection(tableName); + if (collection == null) + return null; + + // System.out.println("update db, query = " + query.toString() + + // ", update = " + update.toString()); + return collection.findAndModify(query, null, null, false, update, true, + true); + } + + public void updateOne(String tableName, DBObject query, DBObject update) { DBCollection collection = db.getCollection(tableName); if (collection == null)