mongoDB日常操作02
db.TABLE_NAME.find({
db.TABLE_NAME.find({
db.TABLE_NAME.find({
db.TABLE_NAME.find({
db.TABLE_NAME.find({
db.TABLE_NAME.distinct({'f1,f2',{
db.TABLE_NAME.distinct({'f1,f2',{
db.TABLE_NAME.aggregate([
{
$lookup:{//连表
from:'table0',
localField:'localField',
foreignField:'foreignField',
as:'table0'
}
},
{
$unwind:'$table0'//扁平化,将数组数据拆分
},
{
$match:{
},
{
$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({
db.TABLE_NAME.update({
db.TABLE_NAME.update({
db.TABLE_NAME.update({
db.TABLE_NAME.update({
$lt 小于;lte 小于等于;$gt 大于;$gte 大于等于;$ne 不等于;$eq 等于
$or:[
{
]
$and:[
{
]
$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(本地导出)