I have a collection with (this is a slight simplification) a unique index on userid and objectid.
Now say objectid 1 is merged into objectid 2. What I'd like to do is update all record that have objectid of 1 to 2, but if I run that as a multi update, and any user is linked to both objects to begin with, the update gives a duplicate key error, and stops updating other records.
Is there any mongodb equivalent to the MySQL "UPDATE IGNORE" (or the mongoDB continueOnError, which seems to apply only to bulk inserts) that will allow me to update the records appropriately in this case? The end result should be that any user who previously had an objectid 1 record will now have an objectid 2 record, and any user that has both records will have the objectid 1 record removed.
I think that I don't understand what the setup is here - could you explain more clearly? Say "merging" object id (_id?) 1 into object id 2 means getting rid of the object id 1 in favor of object id 2. Now all references to 1 should be changed to have the value 2, and presumably you're making the reference with the userid field? But then your unique index on userid forbids more than one reference to 1, so the index constraint error is just your own logic being enforced. In this setup, couldn't there be at most document user referring to any object id, so you just need to remove the reference from at most one document? Perhaps including example documents illustrating the change and the desired result would help.
I think that I don't understand what the setup is here - could you explain more clearly? Say "merging" object id (_id?) 1 into object id 2 means getting rid of the object id 1 in favor of object id 2. Now all references to 1 should be changed to have the value 2, and presumably you're making the reference with the userid field? But then your unique index on userid forbids more than one reference to 1, so the index constraint error is just your own logic being enforced. In this setup, couldn't there be at most document user referring to any object id, so you just need to remove the reference from at most one document? Perhaps including example documents illustrating the change and the desired result would help.
Sure.
First, I was a bit unclear in my first post: the unique index is ONE composite unique index on userid: 1, objectid :1. (objectid is not the same as _id).
so before:
[
{ _id: 10, userid: 1, objectid: 1},
{ _id: 11, userid: 1, objectid: 2},
{ _id: 12, userid: 2, objectid: 1}
]
after:
[
{ _id: 11 (or 10--either would work), userid: 1, objectid: 2},
{ _id: 12, userid: 2, objectid: 2}
]
With mysql, I would be able to do this with two queries: first, update all the objectid = to 2, with UPDATE IGNORE. The second delete any remaining records with objectid = 1.
Followup:
Because I couldn't find any solution to update existing records, I do the following: find all records with objectid 1, and for each record, upsert a new record with the same userid and objectid 2, then delete all records with objectid 1. Clunky, but it works.
댓글 없음:
댓글 쓰기