集合中有四个用户“AB”、“BC”、“CD”、“EF”和十万个 Mongo 文档。
所有文档都有一个名为“AssignedTo”的键,其值为空。
{"AssignedTo":""}
如何以公平的方式将这些用户按顺序分配给 Spring Data 中超过 100 000 条数据?
最佳答案
Mongo shell 具有在其中执行 JavaScript 代码的功能。以下解决方案使用循环策略更新文档的分配者。
登录MongoShell并连接所需的数据库,然后执行以下命令
> var counter = 0
> db.collectionName.find({}).forEach( function(thisDoc) {
... if (counter % 4 == 0) {
... db.collectionName.update({_id: thisDoc._id}, {$set: {"AssignedTo": "AB"}});
... } else if (counter % 4 == 1) {
... db.collectionName.update({_id: thisDoc._id}, {$set: {"AssignedTo": "BC"}});
... } else if (counter % 4 == 2) {
... db.collectionName.update({_id: thisDoc._id}, {$set: {"AssignedTo": "CD"}});
... } else if (counter % 4 == 3) {
... db.collectionName.update({_id: thisDoc._id}, {$set: {"AssignedTo": "EF"}});
... }
... counter = counter + 1;
... })
关于java - 如何为每个 Mongo 文档分配一个用户(字符串值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184552/