mongodb笔记

// 用户
db.createUser({user:'sunkaichao', pwd:'123456',roles:['root']})
db.auth('sunkaichao','123456')
show users
db.dropUser('sunkaichao')
//数据库
db
show dbs
use test
db.dropDatabase()
//集合
db.createCollection('集合名')
db.集合名.insert({})
db.集合名.drop()
show collections
//增
db.集合名.insert({key:'value'})
data = {key:'value',key:'value'}
db.集合名.insert(data)
//删除
db.集合名.remove({name:'value'})
db.集合名.remove({key:'value'},{justOne:true})
db.集合名.remove({})
//修改
db.集合名.update({查询内容,'修改内容'})
# 会将源内容删除,除了id之外
db.集合名.update({查询内容},{$set:{修改内容}},{multi:true})
db.stu.remove({_id:2},{$set:{user:''}})
//基本查询
db.集合名.find()
db.stu.find({_id:1})
db.stu.findOne()
//条件查询
db.stu.find({_id:{$lt:5}})
db.stu.find({_id:{$gt:3}})
db.stu.find({_id:{$lte:4}})
db.stu.find({_id:{$gte:3}})
db.stu.find({_id:{$ne:4}})
//逻辑运算
//$and
db.stu.find(
{
$and:[
{条件1},
{条件2}
]
}
)
db.stu.find({_id:2,age:16})
db.stu.find({$and:[{_id:2},{age:16}]})
//$or
db.stu.find({$or:[{hometown:'大理'},{gender:false} ]})
//混合使用
db.stu.find({$and:[
{$or:[{
hometown:'桃花岛'
},{
age:{$lt:45}
}]},
{
gender:true
}]})
//范围运算符 $in $nin
db.stu.find({hometown:{$in:['大理','内蒙']}})
db.stu.find({hometown:{$nin:['桃花岛']}})
db.stu.find({age:{$nin:[20,16]}})
//正则表达式
db.stu.find({name:/段/})
db.stu.find({name:{$regex:'段'}})
db.stu.find({name:/li/i})
db.stu.find({name:{$regex:'li',$options:'i'}})
//自定义函数$where
db.stu.find({
$where:function (){
return this.age > 20
}
})
//查询结果显示 $skip $limit
db.stu.find().skip(2).limit(2)
db.stu.find().limit(2).skip(2)
//投影 显示:true,不显示:false
db.stu.find({查询条件},{字段:true})
//排序 $sort-1降序,1升序
sort
//统计个数:count()
db.stu.find().count()
//去重: $distinct 返回列表
db.stu.distinct('去重字段',{查询条件})
db.stu.distinct(hometown,{age:{$lt:60}})

//聚合查询:aggregate
//分组,group
db.stu.aggregate([
{$group:{'_id':'$gender'}}
])
//平均$avg
db.stu.aggregate([
{$group:{'_id':'$gender','avg_age':{$avg:'$age'}}}
])
//求和$sum

//取出所有$push
db.stu.aggregate([
{$group:{'_id':'$gender','all_name':{$push:'$name'}}}
])
//match查找符合条件的确别,可以配合管道使用
db.stu.aggregate(
{$match:{age:{$gt:18}}}
)
db.stu.aggregate({$match:{age:{$lt:40}}},{$group:{_id:'$gender', 'avg_age':{$sum:'$age'}}})
//投影 $project
db.stu.aggregate(
{$match:{age:{$gt:20}}},
{$group:{_id:'$hometown',sum_age:{$sum:'$age'},avg_age:{$avg:'$age'}}},
{$project:{sum_age:1}}
)
//排序 $sort-1,1
//将数据列表分割 $unwind
db.stu.aggregate([
{$group:{_id:'$gender',name_list:{$push:'$name'}}},
{$unwind:('$name_list')}
])
//使用循环批量添加测试数据
for (let index = 0; index < 200000; index++) {

db.stu.insert(
{
_id:index,
name:"name"+index,
age:index
}
)
}
//查看查找数据的时间.explain('executionStats')
db.stu.find({_id:'199999'}).explain('executionStats') // 3
db.stu.fin({name:'name199999'}).explain('executionStats') // 79
db.stu.find({age:'age199999'}).explain('executionStats') // 75
//建立索引ensureIndex({字段:1})
db.stu.ensureIndex({name:1})
db.stu.ensureIndex({age:1})
//查询所有的索引getIndexes()
db.stu.getIndexes()
//删除自定义索引
db.stu.dropIndex('name_1')

//备份
mongodump -h 192.168.131.128:27017 -d test -o /home/python/Desktop
//恢复
mongorestore -h 127.0.0.1 -d test2 -o ~/Desktop/test
//数据导出
mongoexport -h 127.0.0.1 -d test -c stu -o xunhuan.csv --type csv -f _id,name,age
mongoexport -h 111111 -d test2 -c stu -o asd.json
//数据导入
mongoimport -d one -c stu --file xunhuan.json --type json

原文地址:https://www.cnblogs.com/Sksitigarbha/p/9507488.html