upsertを実行しますか? これは正常に動作する必要があります。id「ABC」を持つユーザーのための
//suppose that you are trying to get user with Id=abs
Query query = new Query();
query.addCriteria(where(ID).is("abc"));
//and then you can add or update the new field (if the field does not exist, the template adds it into the document)
Update update = new Update();
update.set("addThisField", new Date());
mongo.upsert(query, update, User.class);
基本的には、このクエリ検索。ユーザーがフィールドaddThisFieldを持っている場合、更新を実行します。ユーザーがそのフィールドを持っていない場合は、そのフィールドをドキュメントに追加します。
this.mongoTemplate.update**(<your_criteria>,
Update.fromDBObject(BasicDBObjectBuilder.start("$set",
<your_object>).get()), <output>.class)
サンプル:
BasicDBObject dbObject = new BasicDBObject("a", 1);
Query query = Query.query(Criteria.where("_id").is("123");
Update update = Update.fromDBObject(BasicDBObjectBuilder.start("$set",
dbObject).get());
this.mongoTemplate.updateFirst(query, update, BasicDBObject.class,
"my_collection");
をあなたは基本的に使用している私はあなたがMongoTemplateを使用してマージを実行したい場合は、次の操作を行うことができ、スプリングブーツ1.4
あなたは明確なロジックを持っており、それをurselfで実装しています;) – Jaiwo99