In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
MongoDB how to deal with batch update violates the only constraint of key, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
MongoDB batch update generally uses upsert, but this update may violate the unique establishment constraint and result in no updates at all (the same is true for relational databases). In this case, you can update one by one or judge to update: rows that do not violate the unique constraint can be updated. Here is the way to deal with it, which can be used as a reference for those in need.
[background description]
Collection name: kk_device_likes_game
Unique index: {"device_id": 1
"target_type": 1
"target_id": 1
}
Updated as: db.kk_device_likes_game.update ({"target_type": 5, "target_id": NumberLong (1030)}, {"$set": {"target_id": NumberLong (1031)}}, true,true)
Change the target_id key value of "target_type": 5, "target_id": NumberLong (1030) from NumberLong (1030) to NumberLong (1031)
The update will fail because the collection update field has a compound unique index on it, making the updated key value conflict with other existing key values. As a result, keys that can be updated cannot be updated. The error is as follows:
[problem solving]
In view of the above situation: there are two ideas
Method 1: traverse the set of satisfying conditions and update them one by one
Idea: get the key value that meets the condition, traverse each of its _ id to update one by one, similar to sql is update. Where target_type=5 and target_id=1030 and _ id=...
The syntax is as follows:
Var cursor = db.kk_device_likes_game.find ({"target_type": 5, "target_id": NumberLong (1030)})
While (cursor.hasNext ()) {
Doc=cursor.next ()
A=db.kk_device_likes_game.update ({_ id:doc._id, "target_type": 5, "target_id": NumberLong (1030)}, {"$set": {"target_id": NumberLong (1031)}})
Print (a)
}
Method 2: judge not to update the row if the constraint is violated
Idea: get the key value that meets the condition, and compare the device_ id value with the device_id with the condition target_type=5 and target_id=1031 according to the compound unique index {"device_id": 1, "target_type": 1, "target_id": 1}. If there is the same device_id, the line is not updated. Similar to sql is update. Where target_type=5 and target_id=1030 and device_id not in (select device_id from where target_type=5 and target_id=1031)
The syntax is as follows:
Var cursor = db.kk_device_likes_game.find ({"target_type": 5, "target_id": NumberLong (1030)})
While (cursor.hasNext ()) {
Doc=cursor.next ()
A=db.kk_device_likes_game.find ({device_id:doc.device_id, "target_type": 5, "target_id": NumberLong (1031)}) .count ()
If (a = = 0) {
Db.kk_device_likes_game.update ({_ id:doc._id, "target_type": 5, "target_id": NumberLong (1030)}, {"$set": {"target_id": NumberLong (1031)}})
}
Else {
Print ("_ id:" + doc._id + "cannot be updated in violation of uniqueness constraint")
}
}
[self-check]
After the update, check whether the number of entries that meet the update conditions but are not updated is equal to the number of unique key entries that will be violated after the update. If equal, the update is successful.
View the number of entries that the update violates unique constraints:
ITunes 0
Var cursor = db.kk_device_likes_game.find ({"target_type": 5, "target_id": NumberLong (1030)})
While (cursor.hasNext ()) {
Doc=cursor.next ()
A=db.kk_device_likes_game.find ({device_id:doc.device_id, "target_type": 5, "target_id": NumberLong (1031)}) .count ()
I=i+a
}
The result is 431.
Check the number of entries that meet the update criteria but are not updated:
Db.kk_device_likes_game.find ({"target_type": 5, "target_id": NumberLong (1030)) .count ()
The result is 431.
The two results are the same, and this update is successful!
After reading the above, have you mastered how MongoDB deals with batch update key that violates unique constraints? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un