wangming1993 / issues

记录学习中的一些问题,体会与心得 https://wangming1993.github.io/issues
8 stars 4 forks source link

MongoDB使用 js 更新 collection #12

Open wangming1993 opened 8 years ago

wangming1993 commented 8 years ago
var graphics = db.graphic.find({"accountId":ObjectId("55e44579971374c40a8b4566")})

graphics.forEach(function(g){
    var articles = g.articles;
    var newArticles = [];
    var isNeedUpdate = false;
    articles.forEach(function(a){
        var newArticle = a;
        if ("showCoverPic" in a ) {
            //print("exist showCoverPic");
        } else {
            print("add showCoverPic")
            newArticle.showCoverPic = true;
            isNeedUpdate = true;
        }
        newArticles.push(newArticle);
    })

    if (isNeedUpdate) {
        print(g._id);
        print("need update");
        db.graphic.update({"_id":g._id}, {"$set":{"articles": newArticles}});
    }
});