Mongo 学习笔记
1 - BASE OPERATION
Insert 1
db.people.insert({name:’zhaoyun’, ‘age’: 37});
Insert 2
var person = {name:’purong’, ‘age’:38};
db.people.insert(person);
Insert 3
for (var i=0; i<20; i++) { db.people.insert({name:'user_' + i, age: 30 + i}); }
Remove
db.people.remove({‘name’:’not-exists’});
Remove All
db.people.remove({});
Find All
db.people.find()
Find 1
db.people.find({‘name’:’zhaoyun’});
Find 2
db.people.find({‘name’: {$lt: 38}); // $lt $gt $lge $gte $ne
Find 3
db.people.find({’name’:’zhaoyun’, age:{$lt:38}});
Find 4
db.people.find({$or:[{’name’:’zhaoyun'},{‘age’:{$gt:37}}]});
db.people.find({‘age’: {$in:[37, 38]}}); // $nin
Find 5
db.people.find({‘name’:/.*o.*/});
Find 6
db.people.find({‘name’:/^zh/, ‘name’:/un$/});
Find 7
db.people.find({$where:function() { return this.name==‘zhaoyun'}});
Update 1
db.people.update({’name’: ‘zhaoyun'}, {’name’:’zhaoyun’, ‘age’:37});
Update 2
db.people.update({’name’:’zhaoyun’}, {$inc:{‘age’:1}}); // age += 1
Update 3
db.people.update({’name’:’zhaoyun'}, {$set:{‘age’: 28}});
Update 4: Upsert
// if found, update ‘age’, otherwise insert new document
db.people.update({'name':'purong'},{$set:{'age':38}}, true);
Update 5: Update all
// As default, mongo update will effect on first match document,
// set (true) value for the 4th parameters of update statement to voice: I need update all.
2 - ADVANCE
Count 1
db.people.count();
Count 2
db.people.count({age:{$lt: 38}});
Distinct
db.people.distinct(‘age'); // [ 37, 38 ]
Group 1
db.people.group({key:{age:true}, initial:{num:0}, $reduce:function(doc,prev){prev.num++}});
db.people.group({key: {age:true}, initial: {person:[]}, $reduce:function(cur,prev){prev.person.push(cur.name);}})
Group 2
db.people.group({ key:{age:true}, initial:{ users:[] }, $reduce:function(doc, prev) { prev.users.push(doc.name); },condition:{age:{$lt:35}} });
Group 3
db.people.group({ key:{age:true}, initial:{ users:[] }, $reduce:function(doc, prev) { prev.users.push(doc.name); }, finalize:function(out) { out.xx = out.users.length }, condition:{age:{$lt:35}} });
MapReduce
var map = function() { emit(this.age, {count:1}); }
var reduce = function(key, value) { var result = {count:0}; for (var i=0; i<value.length; i++) { result.count += value[i].count; } return result; }
db.people.mapReduce(map, reduce, { out: 'collection1'})
db.collection1.find()
Cursor
var list = db.people.find();
list.forEach(x => { print(x.name); })
Compose Query
var single=db.people.find().sort({'name',1}).skip(2).limit(2);
Query & Index
db.people.remove({});
for (var i=0; i<100000; i++) { db.people.insert({ name: 'zhaoyun_' + parseInt(i * Math.random()), age:i }); }
db.people.find({name:'zhaoyun_20000'}).explain('executionStats’) // see time used 'executionTimeMillis'
db.people.ensureIndex({ name: 1 }) // 1: Ascending, -1: Descending
db.people.find({name:'zhaoyun_20000'}).explain('executionStats’) // see time used 'executionTimeMillis'
Unique Index
db.person.ensureIndex({"name":1},{"unique":true}) // duplicate value can’t be insert or update
Compose Index
db.people.ensureIndex({ name: 1, age: -1 }) // 1: Ascending, -1: Descending
List & Drop Indexes
db.people.getIndexes();
db.people.dropIndex('name_1');
Master / Slave deploy
// create difference folders for db-master / db-slave
mkdir dbmaster dbslave
// startup 2 mongodb server on difference port
mongod —dbpath=./dbmaster —port=27017 —master &
mongod —dbpath=./dbslave —port=20178 —slave —source=127.0.0.1:27017 &
// connect to master, insert some data
mongo 127.0.0.1:27017
use test
db.people.insert({name:'zhaoyun', age:37});
// connect to slave, see data from master
mongo 127.0.0.1:27018
db.slaveOk(); // as default, slave only do synchronize, can’t read & write
use test
db.people.find()