ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mongoDB日常操作02

2020-09-14 11:03:58  阅读:182  来源: 互联网

标签:02 f1 f2 name -- mongoDB db 日常 table


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(本地导出)

标签:02,f1,f2,name,--,mongoDB,db,日常,table
来源: https://www.cnblogs.com/nnnnmmmm/p/13665492.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有