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}