zoukankan      html  css  js  c++  java
  • mongodb联表查询

    //多表联查
    db.TABLE_NAME.aggregate([
        {
            $lookup:{//连表
                from:'table0',    //被关联的表名
                localField:'localField',    //关联标识符    主动关联的表
                foreignField:'foreignField',    //关联标识符    被关联的表
                as:'table0'        //被关联的表的别名,下面涉及到被关联的表的操作,都用这个
            }
        },
        {
            $unwind:'$table0'//扁平化,将数组数据拆分    被关联的表
        },
        {
            $match:{<query>}//正常查询
            $match:{'or':[<query>,<query>]}//多条件查询    或
        },
        {
            $project:{                //要显示的字段
                '_id':0,    //_id
                'F1':'$table.f1',//取别称    主动关联的表的数据
                'F2':'$table0.f2',    被关联的表的数据
                'F3':{//case when
                    $cond:{if:{$gte:['$f3',30]},then:0,else:50}
                }
            }
        },
    ]).forEach(function(item)){    //遍历    对多表联查出来的数据做操作    一般创建新表
        db.aaaa.insert(item);    //创建新表
    }

    案例:

        打分卡数据库
    1.联表查询与创建新表
    var ls = [];
    db.score_record_r.aggregate([
        {
            '$lookup':{
                'from':'score_parameter_r',
                'localField':'_id',
                'foreignField':'_id',
                'as':'score_parameter_r'
            }
        },
        {'$unwind':'$score_parameter_r'},
        {'$match':{'score_parameter_r.score_parameter.eva1_result':{$in:['shdtax','shdYLian']}}},
        {
            '$project':{
                '_id':'$_id',
                'eva1_result':'$score_parameter_r.score_parameter.eva1_result',
                'name':'$score_record.i2',
                'idno':'$score_record.i3'
             }
         }
    ]).forEach(function(item){
        var insert = true;
        if(ls.indexOf(item.idno)<0){
            ls.push(item.idno);
        }else{
            insert = false;
        }
        if(insert){
            var history = db.tmp0812.findOne({'idno':item.idno});
            if(history&&history.idno){insert = false;}
        }
        insert&&(db.tmp0812.insert(item))
    })

    2.检验新表
    db.tmp0812.find()

    3.导出新表
    ./mongoexport --port 47017 -u root -p root --authenticationDatabase admin -d ruleCard-1211 -c tmp0812 -o tmp0812.json --type json

    -d表示数据库;-c表示数据表;-f需要提取的field,用逗号隔开;--q 导出条件  --o输出路径  --type  数据类型




        信贷工厂数据库
    1.导入新表
    ./mongoimport --port 29034 -u urcbfzd -p urcbfzd --authenticationDatabase admin -d xdgcdb -c tmp0812 --type json --file tmp0812.json
        
    -d表示数据库;-c表示数据表;-f需要提取的field,用逗号隔开;--q 导出条件  --o输出路径  --type  数据类型

    2.联表查询
    db.tmp0812.aggregate([
        {
            '$lookup':{
                'from':'customer_info',
                'localField':'idno',
                'foreignField':'customer_info.d1',
                'as':'customer_info'
            }
        },
        {'$unwind':'$customer_info'},
        {
            '$project':{
                '_id':'$_id',
                'eva1_result':'$eva1_result',
                'name':'$name',
                'idno':'$idno',
                'phone':'$customer_info.customer_info.d',
                'merLicense':{
                    '$ifNull':[
                        '$customer_info.customer_info.i1_content.businessCertNo',
                        '$customer_info.shd_isStock.merLicense'
                    ]
                },
                'merName':{
                    '$ifNull':[
                        '$customer_info.customer_info.i1_content.businessName',
                        '$customer_info.shd_isStock.merName'
                    ]
                 }
            }
         }
    ]).forEach(function(item){
        db.tmp0813.insert(item)  
    })

    3.检验新表
    db.tmp0813.find()

    3.导出新表
    ./mongoexport --port 29034 -u urcbfzd -p urcbfzd --authenticationDatabase admin -d xdgcdb -c tmp0813 -f "eva1_result,name,idno,phone,merLicense,merName" -o tmp0813.csv --type csv


    -d表示数据库;-c表示数据表;-f需要提取的field,用逗号隔开;--q 导出条件  --o输出路径  --type  数据类型


  • 相关阅读:
    p3159 [CQOI2012]交换棋子
    三分法
    p2805 [NOI2009]植物大战僵尸
    p2604 [ZJOI2010]网络扩容
    p1129 [ZJOI2007]矩阵游戏
    有趣与愉快-------罗辑思维整理
    张小龙的书单
    会议
    使用CCProxy代理遇到的问题
    关于看书
  • 原文地址:https://www.cnblogs.com/nnnnmmmm/p/15137435.html
Copyright © 2011-2022 走看看