MongoDB
Win安装
安装路径的根路径下,新建data/db文件夹
运行bin/mongod.exe启动MongoDB Server
运行bin/mongo.exe启动MongoDB命令行
常用命令
1.mongo
2.show dbs
3.use XXX
4.db.dropDatabase()
5.db.getName()
6.db.help()
7.db.copyDatabase(‘name’,’rename’)
8.show collections
9.db.createCollection(‘CollectionName’,{
capped:true//固定集合的大小,超出固定大小,先入先删
,autoIndexId:true//默认false ,自动设置id
,sizi:1000 //配合capped使用,指定集合大小
,max:1000//限定集合上限
})//第二个对象参数可缺省
10.db.xxxxxxCollection.renameCollection(‘renameName’)
11.db.xxxxxxCollection.drop()
12.db.xxxxxxCollection.insert({‘xxxxx’:’xxxxx’});
Insert([{‘xxxxx’:’xxxxx’},{‘xxxxx’:’xxxxx’}])
save([{‘xxxxx’:’xxxxx’},{‘xxxxx’:’xxxxx’}])
insertMany([{‘xxxxx’:’xxxxx’},{‘xxxxx’:’xxxxx’}])
insertOne({‘xxxxx’:’xxxxxxx’})
13.db.xxxxxxCollection.find()
findOne()//查询第一条
find({‘xxxxx’:’xxxxxx’})//条件查询
find({},{‘age’:1})
Find({‘age’:{‘$gt’:20,’$lte’:40}})//大于$gt小于$lt等于$eq大于等于$gte
Find({‘age’:{‘$in’:[28,29,10]}})//$nin取反
Find({‘$or’:[{‘age’:28},{‘user’:’liuxaing’}]})//$and 且
Find({‘age’:{‘$not’:{‘$in’:[29,80,11]}}})
Find({‘comments’:{‘$all’:[{‘xxxx’:’xxxx’}]}})
Find({‘comments’:{‘$size’:3}})
Find({‘user’:’liuxiang’},{‘comments’:{‘$slice’:2}})//正数从前,负数从后
Find({‘vals’:{‘$elemMatch’:{‘>’:1,’<’:3}}})//elemMatch只匹配数组
Find({‘name.f’:’王’,’name.e’:’五’})//文档对象总的对象的查询
Find().limit(3)
Find().skip(2)//忽略
Find().sort({‘age’:1})//排序-1倒叙
14.db.xxxxxxCollection.update({‘xxxxx’:’xxxxxx’},{‘$set’:{‘yyyyy’:’yyyyyy’}},
true//可缺省,检查是否存在,若不存在则新增,默认false
,true//可缺省,开启多数据(批量)模式,默认false
)//更新
15.db.xxxxxxCollection.update({‘xxxxx’:’xxxxxx’},{‘$inc’:{‘yyyyy’:3}})//自增,针对数字
16.var common = {}
db.xxxxxxCollection.update({‘xxxxx’:’xxxxxx’},{‘$push’:{‘yyyyy’:common }})//push对象入数组
17.var common = []
db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$push’:{‘yyyy’:{‘$each’,common}}})//批量push
18.var common = []
db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$push’:{‘yyyy’:{‘$each’,common,’$slice’:-3}}})
19.var comment = []
db.xxxxxxCollection.update({‘xxxxx’:’xxxx’},(‘$addToSet’:{‘yyyy’:{‘$each’:common}}))//批量 去重
20.db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$pop’:{‘yyyy’:-1}})//删除yyyy数组中值,负数从前删除
21.db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$pull’:{‘yyyy’:{‘zzzz’:’zzzz’}}})//条件删除
22.db.xxxxxxCollection.update({‘comments.yyyy’:’xxxx’},{‘$set’:{‘comments.$.yyyy’:’zzzz’}})//下表操作符comments.2.yyyy $适配器
23.db.xxxxxxCollection.ensureIndex({‘age’:1,’name’:1})//添加索引
24.db.xxxxxxCollection.getIndexes()//获取所有索引值
25.db.xxxxxxCollection.find().explain(‘executionStats’)//性能检测