mongo数据库脚本js

const FIX_INCLUDE_FOLDERS = true;

const ownerId = 'me'

function fix(asset,ancestors){
    db.asset.updateOne({_id: asset._id}, {$set:{ancestorIds: ancestors}});
}

function fixData(parentId,ancestorIds){
    let cond = {'ownerId':{$eq:ownerId}};
    cond.parentId = parentId
    printjson(cond);
    let cursor  = db.asset.find(cond);
    if(cursor.size() > 0){ 
        cursor.forEach(asset => {
            if(ancestorIds.indexOf(asset.parentId) == 0){ 
                ancestorIds.push(asset.parentId)
                fix(asset,ancestorIds) 
                cleanData(asset.parentId,ancestorIds) 
            }          
        });
//        cursor.close();
    }
    cursor.close();
}

var ancestorIds = [];
fixData(ownerId,ancestorIds);

一个数据库执行脚本,需要放到工具里面执行:

1、根据条件查询指定信息

2、对信息进行循环,更新

3、递归执行

posted @ 2021-07-20 14:59  凉城  阅读(146)  评论(0编辑  收藏  举报