Mongodb更新问题

时间:2018-05-13 18:54:55

标签: mongodb

我的照片集有5000个这样的文件:

   {
    "id": 1.0,
    "albumId": 1.0,
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "http://placehold.it/600/92c952",
    "thumbnailUrl": "http://placehold.it/150/92c952"
   },
    ...
   {
    "_id": 5000.0,
    "albumId": 9.0,
    "title": "reprehenderit est deserunt velit ipsam",
    "url": "http://placehold.it/600/771796",
    "thumbnailUrl": "http://placehold.it/150/771796"
   }

我尝试更新mongoshell上的url和thumbnailUrl字段,如下面的代码所示。在pics数组中随机取值包含100个值,然后更新照片集中每个文档的url和thumbnailUrl字段:

 var pics = [
     'https://unsplash.com/photos/IGmMRSuh6Qo',
     'https://unsplash.com/photos/t4mQXex4Peo',
     ...
     'https://unsplash.com/photos/bjDpgPvCcM0',
     'https://unsplash.com/photos/6cmZLC4Tq9s'          
    ];
    var rd = (cb)=>{
      return cb(pics[Math.floor((Math.random() * 100) + 1)]);
    }
    for(var i=1;i<=5000;i++){
    rd((res)=>{db.getCollection('photos').updateOne({"_id":i},{$set: 
      {"url":res,"thumbnailUrl":res}});})   
    }

输出:有许多文档具有url的值,而thumbnailURL字段未定义。

enter image description here

需要输出: 应该为url和thumbnailUrl字段更新所有5000个文档的新值。没有文档在url和thumbnailUrl字段中具有未定义的值。

如果我在10和1之间取随机值,则输出正常。但是当我在100和1之间时,它就不行了。我认为这是异步问题。

我通过此链接制作了照片集: https://jsonplaceholder.typicode.com/photos

然后我像这样制作照片数组:

[
'https://unsplash.com/photos/IGmMRSuh6Qo',
'https://unsplash.com/photos/t4mQXex4Peo',
'https://unsplash.com/photos/WF2lvywxdMM',
'https://unsplash.com/photos/gnm2op0m2pw',
'https://unsplash.com/photos/woP_9Ps2RWY',
'https://unsplash.com/photos/XSbJiHcPUAk',
'https://unsplash.com/photos/9KkpIPFWIeM',
'https://unsplash.com/photos/XpqRnutVWCM',
'https://unsplash.com/photos/u535hc1fR8w',
'https://unsplash.com/photos/e3Xfmhfbxj4',
'https://unsplash.com/photos/jNpttMRlb_Y',
'https://unsplash.com/photos/4TET084JWaA',
'https://unsplash.com/photos/EeN8KSwYMjw',
'https://unsplash.com/photos/sYIqGW3hufI',
'https://unsplash.com/photos/tgmGOhXVuoI',
'https://unsplash.com/photos/nYugmV-SY6s',
'https://unsplash.com/photos/f5Y_m2bD_Tw',
'https://unsplash.com/photos/MEZDyn98La8',
'https://unsplash.com/photos/peFXR4binOk',
'https://unsplash.com/photos/SurmPrJ4Rts',
'https://unsplash.com/photos/h-dR-XFJXis',
'https://unsplash.com/photos/-LTxAmtpI5g',
'https://unsplash.com/photos/7bVMdNYzH_8',
'https://unsplash.com/photos/h8wi7UONunE',
'https://unsplash.com/photos/DmUbkltYsKI',
'https://unsplash.com/photos/aEnH4hJ_Mrs',
'https://unsplash.com/photos/FTW8ADj5igs',
'https://unsplash.com/photos/S2YssLw97l4',
'https://unsplash.com/photos/P8M_y-uI3gs',
'https://unsplash.com/photos/1rrCIeGUtcM',
'https://unsplash.com/photos/RBouLnm0L0Q',
'https://unsplash.com/photos/soyhgESoGVc',
'https://unsplash.com/photos/PUVgHyBgZn8',
'https://unsplash.com/photos/CEa3F_sPvqg',
'https://unsplash.com/photos/fJzmPe-a0eU',
'https://unsplash.com/photos/70RHiKO9kj8',
'https://unsplash.com/photos/n2WHxdFtIXU',
'https://unsplash.com/photos/cqrEoS9hRBc',
'https://unsplash.com/photos/cwz9_Pqd8Zs',
'https://unsplash.com/photos/BJAKGX0-9_s',
'https://unsplash.com/photos/hw_sKmjb0ns',
'https://unsplash.com/photos/Pn0XP2Klbcw',
'https://unsplash.com/photos/XsmfoZl6a8Q',
'https://unsplash.com/photos/6vwrVGrxOKE',
'https://unsplash.com/photos/8WODX7nO2JE',
'https://unsplash.com/photos/FWOGqSKq_Cs',
'https://unsplash.com/photos/s_IYpaTaueQ',
'https://unsplash.com/photos/pTrHHGrxeOE',
'https://unsplash.com/photos/xI_-wFJhCiM',
'https://unsplash.com/photos/JckbjvEWQts',
'https://unsplash.com/photos/hDkHSqzGFCA',
'https://unsplash.com/photos/wcNyOYH0_kY',
'https://unsplash.com/photos/0Hf3SuP-Muc',
'https://unsplash.com/photos/8ziKncAPUpY',
'https://unsplash.com/photos/iJY-h1wEMK4',
'https://unsplash.com/photos/FGESRgJ-Aog',
'https://unsplash.com/photos/LmFV2vafmS0',
'https://unsplash.com/photos/GSf90gw9YHw',
'https://unsplash.com/photos/bjDpgPvCcM0',
'https://unsplash.com/photos/6cmZLC4Tq9s',
'https://unsplash.com/photos/WCz-Kp72lpU',
'https://unsplash.com/photos/N7WPtFds0nU',
'https://unsplash.com/photos/qF1oacfAqnI',
'https://unsplash.com/photos/29skLU6lfjo',
'https://unsplash.com/photos/Ixzs6GZwdtY',
'https://unsplash.com/photos/MKd7hcKA4m0',
'https://unsplash.com/photos/NIQ52qeXOBU',
'https://unsplash.com/photos/ush_7RsBAGM',
'https://unsplash.com/photos/KjOTbIIov44',
'https://unsplash.com/photos/tzfdoTi8e2E',
'https://unsplash.com/photos/0Idgp07w-PA',
'https://unsplash.com/photos/8NkBqUQ-nYc',
'https://unsplash.com/photos/TkVqmu1QUSk',
'https://unsplash.com/photos/npk8586on_I',
'https://unsplash.com/photos/4Um0IDn_ax4',
'https://unsplash.com/photos/t--j8FnOUrY',
'https://unsplash.com/photos/1VEVJrBF94U',
'https://unsplash.com/photos/jKvwtbrxzdY',
'https://unsplash.com/photos/--LyFIjXoFY',
'https://unsplash.com/photos/Qdr4mpifNQQ',
'https://unsplash.com/photos/vAD5mhU9JUk',
'https://unsplash.com/photos/D8LcRLwZyPs',
'https://unsplash.com/photos/H5yiRXDUkto',
'https://unsplash.com/photos/_0EALGr5NBs',
'https://unsplash.com/photos/2xCQuiEmBcg',
'https://unsplash.com/photos/0tkmbWNLm9E',
'https://unsplash.com/photos/2xCQuiEmBcg',
'https://unsplash.com/photos/0tkmbWNLm9E',
'https://unsplash.com/photos/yI-GZRkbJ08',
'https://unsplash.com/photos/vVyzufditLQ',
'https://unsplash.com/photos/eHXtoxL1H-E',
'https://unsplash.com/photos/c7qvP413OVU',
'https://unsplash.com/photos/yi9Oytqu5hg',
'https://unsplash.com/photos/nTinYg604lE',
'https://unsplash.com/photos/4KroYsZEfio',
'https://unsplash.com/photos/174ZYtxCtr4',
'https://unsplash.com/photos/4hHLKxPtTfQ',
'https://unsplash.com/photos/cMC_s7e150o',
'https://unsplash.com/photos/pNH6_AOXTnU',
'https://unsplash.com/photos/Ul07QK2AR-0'   
]

1 个答案:

答案 0 :(得分:1)

您可以使用forEach逐个更新文档。据我所知,你想用数组的随机元素更新你的字段,你可以像下面这样做:

 var pics = [
     'https://unsplash.com/photos/IGmMRSuh6Qo',
     'https://unsplash.com/photos/t4mQXex4Peo'
     ]

function random() {
    return (pics[Math.floor(Math.random()*pics.length)]);
}

db.COLLECTION_NAME.find().forEach( function(myDoc) {
    const res = random()
         db.getCollection("COLLECTION_NAME").update({ _id: ObjectId(myDoc._id) }, {
    $set: {
        "YOUR_FIELD_NAME": res
    }
    })
} );

希望它适合你。