1.与关系型数据库(MySQL)的对比
document(文档)<-> 一行记录
collection(集合) <-> Table(表)
database(数据库)<-> database
2.crud操作
创建
> user = {'name': 'sosop', 'age': 18, 'gender': 'male'} { "name" : "sosop", "age" : 18, "gender" : "male" } > db.users.insert(user) WriteResult({ "nInserted" : 1 })
查询
db.users.insert(user) WriteResult({ "nInserted" : 1 }) > db.users.find() { "_id" : ObjectId("549557d23359eef64b07bb46"), "name" : "sosop", "age" : 18, "gender" : "male" } > db.users.findOne() { "_id" : ObjectId("549557d23359eef64b07bb46"), "name" : "sosop", "age" : 18, "gender" : "male" }
更新
user.birthday = new Date('1996-01-01') ISODate("1996-01-01T00:00:00Z") b.users.update({'name': 'sosop'},user) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549557d23359eef64b07bb46"), "name" : "sosop", "age" : 18, "gender" : "male", "birthday" : ISODate("1996-01-01T00:00:00Z") }
删除
db.users.remove({'name': 'sosop'}) WriteResult({ "nRemoved" : 1 })
3.修改器的使用
$set:键不存在则创建,存在则修改
> db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 18, "gender" : "male", "birthday" : ISODate("1996-01-01T00:00:00Z") } > db.users.update({'name': 'sosop'}, {$set: {'birthday': new Date('1996-03-11')}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 18, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z") } > db.users.update({'name': 'sosop'}, {$set: {'email': 'sosopish@163.com'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 18, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com" }
$inc:增加减少
> db.users.update({'name': 'sosop'}, {$inc: {'age': 10}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.findOne() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 28, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com" } > db.users.update({'name': 'sosop'}, {$inc: {'age': -2}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com" }
$push
> db.users.update({'age': 26}, {$push: {'phone': "88888888888"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888" ]
$addToSet
db.users.update({'name': 'sosop'}, {$addToSet: {'phone': '66666666666'}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666" ] } db.users.update({'name': 'sosop'}, {$addToSet: {'phone': {$each: ['99999999999', '000000000']}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999", "000000000" ] }
$pop:{key: 1} 从尾部删除 {key: -1}从头部删除
$pull:{key: value} 删除指定元素
数组定位修改器
db.users.update({'name': 'sosop'}, {$set: {'phone.$.xxx': 'xxx'}}
4.upsert更新或创建,同时避免静态
update({}, {}, true)
save: 存在时更新,否则插入
update({}, {}, true, true): 多文档更新
5.查询
find() find({}) find({}, {field: 1|0...})
> db.users.find({'age': {$gte: 18, $lte: 30}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] }
$lt $gt $lte $gte $ne
$in $nin
db.users.find({'phone': {$in: ['666666666', '66666666666']}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] }
$or
> db.users.find({$or: [{'name': 'sosop'}, {'age': 18}]}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] }
$not $mod
6.特定类型查询
null
> db.users.find({'name': {$exists: true}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] } > db.users.find({'addr': {$exists: true}}) >
reg:
db.users.find({'name': /sosop/}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] } db.users.find({'name': /\S/i}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ]
数组查询
> db.users.find({'phone': {$all: ['88888888888', '99999999999']}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] }
> db.users.find({'phone': {$size: 3}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] }
> db.users.find({'name': 'sosop'},{'phone': {$slice: 1}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888" ] } > db.users.find({'name': 'sosop'},{'phone': {$slice: 2}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666" ] } > db.users.find({'name': 'sosop'},{'phone': {$slice: -1}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "99999999999" ] } > db.users.find({'name': 'sosop'},{'phone': {$slice: [2,3]}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "99999999999" ] } > db.users.find({'name': 'sosop'},{'phone': {$slice: [1,2]}}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "66666666666", "99999999999" ] }
内嵌文档
db.users.find({'addr.city': 'Chengdu'}) db.users.find({'addr': {$elemMatch: {'city': 'Chengdu', 'country': 'China'}}})
$where查询比常规长训慢
7.游标
> var cursor = db.users.find() > while(cursor.hasNext()) { ... u = cursor.next() ... print(u.name) ... } sosop > cursor.forEach(function(u) { ... print(u.age) ... } ... )
limit skip sort
> db.users.find() { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] } { "_id" : ObjectId("549577f83359eef64b07bb48"), "name" : "net-sosop", "age" : 27, "gender" : "male", "birthday" : ISODate("1996-01-01T00:00:00Z") } > db.users.find().limit(1) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] } > db.users.find().skip(1) { "_id" : ObjectId("549577f83359eef64b07bb48"), "name" : "net-sosop", "age" : 27, "gender" : "male", "birthday" : ISODate("1996-01-01T00:00:00Z") } > db.users.find().sort({'age': 1}) { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] } { "_id" : ObjectId("549577f83359eef64b07bb48"), "name" : "net-sosop", "age" : 27, "gender" : "male", "birthday" : ISODate("1996-01-01T00:00:00Z") } > db.users.find().sort({'age': -1}) { "_id" : ObjectId("549577f83359eef64b07bb48"), "name" : "net-sosop", "age" : 27, "gender" : "male", "birthday" : ISODate("1996-01-01T00:00:00Z") } { "_id" : ObjectId("549567573359eef64b07bb47"), "name" : "sosop", "age" : 26, "gender" : "male", "birthday" : ISODate("1996-03-11T00:00:00Z"), "email" : "sosopish@163.com", "phone" : [ "88888888888", "66666666666", "99999999999" ] }
避免使用skip过滤大量数据
8.高级查询
$maxscan : integer
$max : document
$min :document
$explain: boolean
$hint: documnet
$snapshot: boolean
9.索引
建立索引
db.users.ensureIndex({'name': 1})
注意多键创建索引时,索引的方向
索引缺点:每次cud时都会有额外的开销
索引名称:
db.users.ensureIndex({'name': 1}, {‘name’: 'name'm 'unique': true, 'dropDups': true})
explain和hint
db.users.find().explain()
10.地理空间索引
db.users.ensureIndex({'gps': '2d'}, {$min: 100,$max: 1000}) db.users.find({'gps': {$near: [200,80]}})
$within $box $center
11.聚合
count
db.users.count({})
group
db.users.group({key: {age: true}, initial: {"age": 0}, $reduce: function(doc, prev){ if (doc.age > prev.age) { prev.age = doc.age }}, condition: {age: {$gt: 26}}})
finalize完成器
函数作为键:$keyf: function(x) {}
MapReduce:
并行化到多个服务器的聚合方法
map reduce finalize keeptemp output query limit sort scope verbose
db.users.mapReduce(map, reduce, {out: 'tmp', sort: {age: 1}, limit: 1}
相关推荐
mongodb常用命令大全,包括大部分的命令,从安装到使用
MongoDB常用命令批处理,包括安装服务/启动服务/带授权认证的服务/移除服务等。其中.bat为windows平台,.sh为linux平台(只测过centos7)
Mongodb常用命令和java调用 比较好噢
MongoDB常用命令
MongoDB常用操作命令大全 数据库常用命令 Collection聚集集合 用户相关 聚集集合查询
mongodb php distinct command --- mongoDb 常用命令
在centos7下安装部署mongodb分片+副本集群常用命令整理,内容包含,安装、配置、启动、访问shell终端等命令
包含对数据库、集合、文档的常用操作。
MongoDB常用的操作命令
mongodb 操作命令(全) 包括: 数据库相关的操作 集合相关操作 用户相关操作 。。。。。
附件是mongodb 数据库基本操作,包含最常用的 15 条命令,非常适合MongoDB入门级学习使用,文件绿色安全,仅供学习交流使用,无任何商业目的,欢迎大家下载使用!
MongoDB常用SQL操作
4、删除当前使用数据库 db.dropDatabase() 8、查看当前使用的数据库 db.getName() 2、得到指定名称的聚集集合(table) db.
MongoDB数据库常用命令.docx
查看当前数据库中所有的集合,使用命令 show collections 或使用 show tables 创建集合有两种方式,显示创建和隐式创建 显示创建可以使用命令 db.createCollection(“集合名称") 隐式创建可以使用命令 db.集合...
MongoDB常用SQL操作,包括dml 和ddl语句,每条sql语句都有对应的示例
mongodb常用管理命令.pdf
主要介绍了mongodb安装、常用命令、举例、数据导入导出及备份恢复,适用于初学者学习使用。
囊括了mongodb部署,检测,优化,工具操作的命令大全