mongodb javascript脚本案例

  • mongodb js脚本
    • 实例
      • 统计用户
      • 更新数据库
      • 多表统计

mongodb js脚本

实例

统计用户

//链接数据库
var mapmonitor = connect("mapmonitor");
var mappushdb = connect("mappush");
var userMonthLogin = mapmonitor.runCommand({distinct:'loginMonitor',key:'user_name',query:{"appKey":"fb27d257e0d464d63d200d9e7c2ded2a","result":"true","monitor_date":{"$gte":new Date(Date.parse(new Date()) - 86400000 * 30)}}});

var arr = userMonthLogin.values;

for(var i=0 ; i< arr.length; i++){
    var name = arr[i];
    var userlist = mappushdb.imMessageUser.find({"username":name});
    if(userlist.hasNext()){

    }else{
        mappushdb.imMessageUser.save({"username":name,"createTime":new Date(),"updateTime":new Date(),"state":"0"});
    }
}

更新数据库

//链接数据库
var mappushdb = connect("mappush");

var imPushRequestList = mappushdb.imPushRequest.find({"state":"sent","retry_time":{"$lt":new Date(),"$gte":new Date(Date.parse(new Date()) - 86400000 * 2)}});

while(imPushRequestList.hasNext()){
    var temp = imPushRequestList.next();

    var messageList = mappushdb.imMessage.find({"msgId" : temp.msgId});
    //print (message.next());
    if(messageList.hasNext()){
        var messagetemp = messageList.next()
        var name = messagetemp.to;
        var userList = mappushdb.imMessageUser.find({"username":name});
        if(messageList.hasNext()){

        }else{
            print("no:"+temp.msgId);
            mappushdb.imPushRequest.update({"_id":temp._id},{$set:{"state":"failed"}});
            break;
        }
    }
}

多表统计

var mideadb = connect("midea");
//找到所有模块的类型
var categoryList = mideadb.settingCategory.find({"type":"2"},{"_id":1,"name":1});

while(categoryList.hasNext()){
    var categorytemp = categoryList.next();
    var categoryId = categorytemp._id.str;
    var categoryName = categorytemp.name;
    //print(categorytemp._id.str);
    var widgetList = mideadb.widget.find({"category_id":categoryId});
    //print(widgetList.hasNext());
    while(widgetList.hasNext()){
        var widgettemp = widgetList.next();
        var widgetId = widgettemp._id.str;
        var widgetName = widgettemp.name;
        var widgetIdentify = widgettemp.identify;

        var widgetVersionList = mideadb.widgetVersion.find({"widget_id":widgetId});

        while(widgetVersionList.hasNext()){
            var widgetVersionTemp = widgetVersionList.next();
            var widgetVersionId = widgetVersionTemp._id.str;

            var appVersionList = mideadb.appVersionWidgetVersion.find({"widgetVersionId":widgetVersionId});

            var appVersionLinkResult="";
            while(appVersionList.hasNext()){
                var appVersionTemp = appVersionList.next();
                var appVersionId = appVersionTemp.appId;

                var appList = mideadb.appMain.find({"_id":ObjectId(appVersionId)});
                //查找应用的名称
                var appname = '';
                if(appList.hasNext()){
                    var apptemp = appList.next();
                    appname = apptemp.app_name;

                }
                //拼接字符串
                appVersionLinkResult += appname+"-"+appVersionTemp.appVersion+"-"+appVersionTemp.platform;
                appVersionLinkResult += ";";
            }

            //输出结果
            print(categoryName+","+widgetName+","+widgetIdentify+","+widgetVersionTemp.version+","+appVersionLinkResult);

        }
        //break;
    }
    //break;
}

你可能感兴趣的:(mongodb)