new Update(SomeModel.class) .set("Enabled = 0") .where("Account = ?", account.getId()) .execute();
String updateSet = " UserAnswer = ? ," + " UserAnswerType = ? "; new Update(Question.class) .set(updateSet, answerID, userAnswerType) .where(" QID = ? ", questionID) .execute();
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; ... Update update = new Update(); update.addToSet("posts", post); Criteria criteria = Criteria.where("_id").is(id); template.updateFirst(Query.query(criteria), update, "business");
String x="sent = 1,file = "+n.getString("file"); new Update(Messages.class) .set(x) .where("id = ?", lid) .execute();
Where update = new Update().table(Ant.class).set(Condition.column(Ant$Table.TYPE).eq("other")) .where(Condition.column(Ant$Table.TYPE).is("worker")) .and(Condition.column(Ant$Table.ISMALE).is(true)); update.queryClose();
import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query; import static org.springframework.data.mongodb.core.query.Update; ... WriteResult wr = mongoTemplate.updateMulti( new Query(where("commentsList.id").is("123")), new Update().inc("commentsList.$.numberOfLikes", 1), MongoPost.class );
mongoTemplate.updateFirst(collection, ... new Query(where("_id").is(...)), ... new Update().push("field", dbo));
MongoOperations mongoOperations = mongoConfiguration.getMongoTemplate(); Query query = new Query(where("_id").is(docID).and("itemList.itemID").is(itemID)); Update update = new Update().pull("itemList", new BasicDBObject("itemID", "1")); mongoOperations.updateFirst(query, update, Item.class);
Query query = query( new Criteria().andOperator( where("_id").is(conversation.getId()), where("messages._id").is(message.getId()) ) ); mongoOps.upsert(query, new Update().set("messages.$.text", "coolest text"), Conversation.class);
import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query; import static org.springframework.data.mongodb.core.query.Update; ... WriteResult wr = mongoTemplate.updateFirst(new Query(where("accounts.accountType").is(Account.Type.SAVINGS)), new Update().pull("students.$.scores", new Document("type", "homework")), Account.class);
final Query query = new Query(new Criteria().andOperator( Criteria.where("_id").is("id"), Criteria.where("listA").elemMatch(Criteria.where("_id").is("id")) )); final Update update = new Update().addToSet("listA.$.listB", stuff).set("listA.$.thing", "thing"); final WriteResult wr = mongoOperations.updateFirst(query, update, "collectionName");
Intrest insertObj = new Insert(); //initilize insert obj here .. Update args = new Update(); args.addToSet("intrest",insertObj); Query query = new Query(Criteria.where("id").is("527242d584ae917d8bd75c7b")); // if u want to do upsert mongoOperation.findAndModify(query, args, FindAndModifyOptions.options().upsert(true), Post.class); //if u want to just update mongoOperation.findAndModify(query, args, Post.class);
public Pair<Data> saveItems(String id, List<Item> items) { final Query findById = ...; final Update update = new Update(); // This is what we actually update update.set(ENTITY_FIELD_ITEMS, newItems); update.inc(ENTITY_FIELD_VERSION, 1); // Try updating and return the old data final Data oldData = findById(); //query to retrieve existing data final Data newData = operations.findAndModify(findById, update, FindAndModifyOptions.options().upsert(true).returnNew(true), Data.class); return new Pair<Data>(oldData, newData); }
Query findQuery = new Query(); Criteria findCriteria = Criteria.where("idVar").is(idVar); findQuery.addCriteria(findCriteria); Update update = new Update().pull("listaHoras", new BasicDBObject( "$gte", start).append("$lte", end)); mongoOps.updateMulti(findQuery, update, "CollectionName");
public Update createStatement(String sql) { return new Update(this, statementLocator, statementRewriter, statementBuilder, sql, new ConcreteStatementContext(globalStatementAttributes), log, timingCollector, foreman, containerFactoryRegistry); }
Query query = new Query(Criteria.where("id").is(id)); query.fields().slice("scores", -3); Update update = new Update(); update.push("scores", scores); update.push("scores", new BasicDBObject("$each", scores).append("$slice", -3)); mongoOperations.findAndModify(query, update, MyDocument.class);
Update update = new Update(); JSONObject jsonObject = new JSONObject(new SomeClass()); update.set("field",JSON.parse(jsonObject.toString())); update.unset("otherField"); System.out.println(update.getUpdateObject().toString());
//return Update object public static Update fromDBObjectExcludeNullFields(DBObject object) { Update update = new Update(); for (String key : object.keySet()) { Object value = object.get(key); if(value!=null){ update.set(key, value); } } return update; } //build udpate Update update = fromDBObjectExcludeNullFields(dbDoc);
Query query = new Query(); query.addCriteria(Criteria.where("_id").is(new ObjectId("10-100-5675234"))); Update update = new Update(); update.unset("userId"); //the fields you want to remove update.set("putInYourFieldHere", "putInYourValueHere"); //the fields you want to add mongoTemplate.updateFirst(query, update, Device.class);
MongoOperations mongoOperations; //inject Query query = new Query(); Update update=new Update(); update.set("quantity", materialRequest.getQuantity()); update.set("usrId", materialRequest..getUsrId()); query.addCriteria(Criteria.where("ticketId").is(materialRequest.getTicketId()).and("materialId").is(materialRequest.getMaterialId())); long count = mongoOperations.count(query, MaterialRequest.class); if (count <= 0) { mongoOperations.save(materialRequestList, MongoCollections.MATERIAL_REQUEST_COLLECTION); } else { mongoOperations.updateFirst(query, update, MaterialRequest.class); }