Reputation: 19260
Team,
I have to create the below document when the document not exists (find_on_user).
{
"st" : "s", //current status s=sent, d=delivered, u=undelivered, r=resent
"u" : "user",
"e" : [
//HISTORY OF EVENTS "s" - status at the time of each event
{"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"s", "@":ISODate("2017-01-29T08:43:00Z")}
]
}
If exists, for successive above events i have to update only the event array. Note latest status("st") in the main document also getting updated as per the new event added into the array.
{
"st" : "r", //current status s=sent, d=delivered, u=undelivered, r=resent
"u" : "user",
"e" : [
//HISTORY OF EVENTS "s" - status at the time of each event
{"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"s", "@":ISODate("2017-01-29T08:53:00Z")},
{"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"u", "@":ISODate("2017-01-29T09:43:00Z")},
{"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"r", "@":ISODate("2017-01-29T10:43:00Z")}
]
}
I think what i want is upsert along with $push
db.collection.update(query, update, {upsert: true})
DBObject listItem = new BasicDBObject("e", new BasicDBObject("c","2b3de827-ac38-4023-5950-819947858eac").append("s","r").append("@",ISODate("2017-01-29T10:43:00Z"));
DBObject updateQuery = new BasicDBObject("$push", listItem);
myCol.update(findQuery, updateQuery);
How to do this together in mongodb spring data? Please help.
Question is something very similar to mongodb - create doc if not exist, else push to array
But this is not telling about how to update outer document("st") along with $push? also this answer is dealing with String Json, is that possible via a java object?
From Veeram Answer:
Query query = new Query(Criteria.where("u").is("user"));
DBObject listItem = new BasicDBObject("c","2b3de827-ac38-4023-5950-819947858eac").append("s","r").append("@",new Date());
Update update = Update.update("st","r").push("e", listItem);
mongoOperations.upsert(query, update, collection_name);
since i have only two params like "u", "st" Veeram has used one at query and one at update. What will happen when i have more than two, but those to be updated only at the time of insert, later only status("st") get updated.
Example below "type" and "account_id" will be added at the time of insert, later update will be done only at "st". How to solve this?
{
"st" : "s", //current status s=sent, d=delivered, u=undelivered, r=resent
"u" : "user",
"type" : "welcome-sms",
"account_id" : "12as-df23-fg-1",
"e" : [
//HISTORY OF EVENTS "s" - status at the time of each event
{"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"s", "@":ISODate("2017-01-29T08:43:00Z")}
]
Do i need to use like below? But type and account_id are static. needs to be updated only at first(insert) time. Is that fine?
Update updateObj = Update.update("st","r")
updateObj.set("type", "welcome-sms");
updateObj.set("account_id", "12as-df23-fg-1").push("e", listItem);
Upvotes: 0
Views: 872
Reputation: 75934
You can try something like this.
Query query = new Query(Criteria.where("u").is("user"));
DBObject listItem = new BasicDBObject("c","2b3de827-ac38-4023-5950-819947858eac").append("s","r").append("@",new Date());
Update updateObj = Update.update("st","r")
updateObj.setOnInsert("type", "welcome-sms");
updateObj.setOnInsert("account_id", "12as-df23-fg-1").push("e", listItem);
mongoOperations.upsert(query, updateObj, collection_name);
Upvotes: 1