From e113580e99acefb9f48a0329a2065dc0598a6b34 Mon Sep 17 00:00:00 2001 From: qianwang5 Date: Wed, 21 Mar 2018 09:51:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0findAndUpdate=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5upsert?= =?UTF-8?q?=E5=92=8CreturnNew?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../senseframework/support/orm/mongodb/Mongo.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sense-support-mongodb/src/main/java/org/senseframework/support/orm/mongodb/Mongo.java b/sense-support-mongodb/src/main/java/org/senseframework/support/orm/mongodb/Mongo.java index dd75cbc..8d910ca 100755 --- a/sense-support-mongodb/src/main/java/org/senseframework/support/orm/mongodb/Mongo.java +++ b/sense-support-mongodb/src/main/java/org/senseframework/support/orm/mongodb/Mongo.java @@ -1,6 +1,7 @@ package org.senseframework.support.orm.mongodb; -import com.mongodb.*; +import com.mongodb.WriteConcern; +import com.mongodb.WriteResult; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.senseframework.support.orm.mongodb.exception.QueryException; @@ -10,6 +11,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.geo.GeoResult; import org.springframework.data.geo.GeoResults; import org.springframework.data.geo.Metrics; +import org.springframework.data.mongodb.core.FindAndModifyOptions; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.NearQuery; @@ -439,6 +441,14 @@ public class Mongo extends MongoQuery { return (T) mongoOperations.findAndModify(query, _update, clazz, collectionName(clazz)); } + public T findAndUpdate(Class clazz, IMongoUpdate update, boolean upsert, boolean returnNew) { + Update _update = new Update(); + update.update(_update); + init(); + FindAndModifyOptions options = new FindAndModifyOptions(); + options.upsert(upsert).returnNew(returnNew); + return (T) mongoOperations.findAndModify(query, _update, options, clazz, collectionName(clazz)); + } public T one(Class clazz) { init(); -- Gitee