• mongoDB日常操作02


    db.TABLE_NAME.find({<query>})//普通查询
    db.TABLE_NAME.find({<query>},{'_id':0,'f1':1,'f2':1})//只返回f1、f2,不返回其他字段,全1,默认返回_id,可设定_id:0,使_id不返回
    db.TABLE_NAME.find({<query>},{'f1':0,'f2':0})//不返回f1、f2,返回其他字段,全0
    db.TABLE_NAME.find({<query>}).pretty()//格式化查询
    db.TABLE_NAME.find({<query>}).count()//计数
    db.TABLE_NAME.distinct({'f1,f2',{<query>}})//去重
    db.TABLE_NAME.distinct({'f1,f2',{<query>}}).length//去重计数
    db.TABLE_NAME.aggregate([
        {
            $lookup:{//连表
                from:'table0',
                localField:'localField',
                foreignField:'foreignField',
                as:'table0'
            }
        },
        {
            $unwind:'$table0'//扁平化,将数组数据拆分
        },
        {
            $match:{<query>}//查询语句
        },
        {
            $project:{
                '_id':0,//去掉_id
                'F1':'$table.f1',//取别称
                'F2':'$table0.f2',
                'F3':{//case when
                    $cond:{if:{$gte:['$f3',30]},then:0,else:50}
                }
            }
        },
    ])
    //$set:set
    $unset:移除
    $inc:自增,带自增量级参数
    db.TABLE_NAME.update({<query>},{$set:{},$unset:{},$inc:{'f1':inc}})//更新一条
    db.TABLE_NAME.update({<query>},{$set:{}},false,true)//全部更新,未查询到结果不做操作
    db.TABLE_NAME.update({<query>},{$set:{}},true,false)//添加一条,未查询到结果则插入
    db.TABLE_NAME.update({<query>},{$set:{}},true,true)//添加所有
    db.TABLE_NAME.update({<query>},{$set:{}},false,false)//更新一条


    $lt 小于;lte 小于等于;$gt 大于;$gte 大于等于;$ne 不等于;$eq 等于
    $or:[
        {<query1>},{<query2>},...
    ]
    $and:[
        {<query1>},{<query2>},...
    ]
    $not:{}//取反
    $in:[//in
        'value1','value2',...
    ]


    ./mongoimport -d xdgcdb -c table_name --type json --file table_name.json(本地导入)
    ./mongoexport -d xdgcdb -c table_name -o table_name.json --type json(本地导出)

    //导出json
    ./mongoexport --port ***** -u ****** -p ****** --authenticationDatabase admin -d xdgcdb -c table_name -o table_name.json --type json
    //导出csv
    ./mongoexport --port ***** -u ****** -p ****** --authenticationDatabase admin -d xdgcdb -c table_name -f "_id,f1,f2,f3,..." -q "{}" -o table_name.csv --type csv
    //导入json
    ./mongoimport --port ***** -u ****** -p ****** --authenticationDatabase admin -d xdgcdb -c table_name --type json --file table_name.json
    //导入csv
    ./mongoimport --port ***** -u ****** -p ****** --authenticationDatabase admin -d xdgcdb -c table_name --type csv --headerline --file table_name.csv

    //创建以f1升序f2降序,名为index_f1_f2,自动在180秒后清除的索引
    db.collection.createIndex({'f1':1,f2:-1}, {name:'index_f1_f2',expireAfterSeconds: 180})
    //查看所有索引
    db.col.getIndexes()
    //查看索引大小
    db.col.totalIndexSize()
    //删除所有索引
    db.col.dropIndexes()
    //查看名为index_f1_f2的索引
    db.col.dropIndex("index_f1_f2")
    //创建数组索引
    db.c.ensureIndex({"f1":1,"f2":1,"f3":1})
    //创建固定集合,集合大小为10000k,容量为1000条,当大小或容量满出时,覆盖第一条记录,可用于日志记录
    db.createCollection("c1",{capped:true,size:10000,max:1000})
    //判断集合是否为固定集合
    db.c2.isCapped()
    //将以存在的集合转为固定集合
    db.runCommand({"convertToCapped":"c2",size:10000})

    查询并删除当前时间之前的数据
    db.lcpt_part_info.remove({'object._s.ut':{'$lt':'2020-06-02 23:59:59'}})
    查询并删除当前时间之后的数据
    db.lcpt_group_info.remove({'object._s.ut':{'$lt':'2020-06-02 23:59:59'}})

    查询并更新数据  set更新
    db.ptd_business_group.update({'ptd_business_group.partname':'团长姓名'},{$set:{'ptd_business_group.endtime':'2020-05-25 15:59:14'}})

    多表查询并更新/删除数据
    db.ptd_business_part.find({'ptd_business_part.name':'刘维巍','ptd_business_part.idno':'500102198811280513'}).forEach(
        function(item){
            db.ptd_business_group.update({_id:item.ptd_business_part.groupid},{$set:{'ptd_business_group.group.查询到的团员表id':'','ptd_business_group.cgsize':'1','ptd_business_group.ngsize':'4'}},true)
        }
    );

    单条数据导入或导出
    导出
    ./mongoexport --port 29034 -u 账户名 -p 密码 -- authenticationDatabase admin -d 库名-c 表名 -q '{"查询语句"}' -o 导出名以及地址.json --type json(本地导出)

  • 相关阅读:
    表单高级和表格高级
    XHTML基础知识
    浅析JavaScript访问对象属性和方法及区别
    互联网盈利模式研习笔记之二:佣金与分成
    互联网盈利模式研习笔记之一:流量变现
    前端两年开发经验需要了解的常识!
    拖拽原理
    js中常见兼容性
    前端性能优化
    JS框架常识。
  • 原文地址:https://www.cnblogs.com/nnnnmmmm/p/13665492.html
Copyright © 2020-2023  润新知