馆藏有四个用户"AB“、"BC”、"CD“、"EF”和10万份蒙古文件。
有一个名为"AssignedTo“的键,所有文档的值都为空。
{"AssignedTo":""}如何公平地将这些用户按顺序分配给Spring数据中的>10万个数据?
发布于 2019-07-24 19:33:32
Mongo具有在其内部执行JavaScript代码的功能。下面的解决方案使用Robin策略更新文档的AssignedTo。
登录到MongoShell并与所需的db连接,然后执行以下命令
> 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;
... })https://stackoverflow.com/questions/57184552
复制相似问题