Can I do a .save() with a pop in one operation?

36 views
Skip to first unread message

cgrchen

unread,
Aug 13, 2012, 8:54:26 PM8/13/12
to mor...@googlegroups.com

I'm currently having a technical challenge that you might be able to help. I've been using the .save() alongside @Version. I have Set, Map, and List in the POJO object.


Now I have a necessity to save every property of that collection entry, but for just one collection. For example, if user A gets a gift from user B and C while claiming gift from D concurrently, then I would like to do these operations:

1. Save everything in A and pop D from the "gifts" property. So if the gift is money, here's the query:

{"_id":"A", "version":100000000}, {"version":100000001, "money":123123, {$pop:{"gifts":"D"}}

2. Insert gift from B (no need for versioning)

{"_id":"A"}, {$push:{"gifts":"B"}}

3. Insert gift from B (no need for versioning)

{"_id":"A"}, {$push:{"gifts":"C"}}

 

I know I can do #2 and #3 easily, but is there a way to do #1?

Scott Hernandez

unread,
Aug 14, 2012, 1:24:00 AM8/14/12
to mor...@googlegroups.com
For one, you will need to form the update yourself like this, instead
of using save:

ds.update(entity, ds.createUpdateOperations(Entity.class).set("money",
xxxx).removeFirst("Gifts"))

This will use the version to do the query and update the version for
you in the update operations. Please give it a try and let me know if
you have any issues.

Please note that $pop (or remove* in morphia) doesn't take a
query/value just a field name; I think you might want pull or
removeAll.
http://www.mongodb.org/display/DOCS/Updating#Updating-%24pop
Reply all
Reply to author
Forward
0 new messages