./mongod
./mongo
show dbs 或show databases
use learn
show collections 或show tables
P5
db.system.indexes.find()
db.unicorns.insert({name: 'Leto', gender: 'm', home: 'Arrakeen', worm: false}
db.unicorns.remove()
db.unicorns.find({gender: 'm', weight: {$gt: 700}})
db.unicorns.find({gender: {$ne: 'f'}, weight: {$gte: 701}})
db.unicorns.find({vampires: {$exists: false}})
db.unicorns.find({gender: 'f', $or: [{loves: 'apple'}, {loves: 'orange'}, {weight: {$lt: 500}}]})
db.unicorns.find({_id: ObjectId(”TheObjectId”)})
P6 准备数据
db.unicorns.insert({name: 'Horny', dob: new Date(1992,2,13,7,47), loves: ['carrot','papaya'],weight: 600, gender: 'm', vampires: 63});
db.unicorns.insert({name: 'Aurora', dob: new Date(1991, 0, 24, 13, 0), loves: ['carrot', 'grape'], weight: 450, gender: 'f', vampires: 43});
db.unicorns.insert({name: 'Unicrom', dob: new Date(1973, 1, 9, 22, 10), loves: ['energon', 'redbull'], weight: 984, gender: 'm', vampires: 182});
db.unicorns.insert({name: 'Roooooodles', dob: new Date(1979, 7, 18, 18, 44), loves: ['apple'],weight: 575, gender: 'm', vampires: 99});
db.unicorns.insert({name: 'Solnara', dob: new Date(1985, 6, 4, 2, 1), loves:['apple', 'carrot', 'chocolate'], weight:550, gender:'f', vampires:80});
db.unicorns.insert({name:'Ayna', dob: new Date(1998, 2, 7, 8, 30), loves: ['strawberry', 'lemon'], weight: 733, gender: 'f', vampires: 40});
db.unicorns.insert({name:'Kenny', dob: new Date(1997, 6, 1, 10, 42), loves: ['grape', 'lemon'],weight: 690,gender: 'm', vampires: 39});
db.unicorns.insert({name: 'Raleigh', dob: new Date(2005, 4, 3, 0, 57), loves: ['apple', 'sugar'],weight: 421, gender: 'm', vampires: 2});
db.unicorns.insert({name: 'Leia', dob: new Date(2001, 9, 8, 14, 53), loves: ['apple', 'watermelon'], weight: 601, gender: 'f', vampires: 33});
db.unicorns.insert({name: 'Pilot', dob: new Date(1997, 2, 1, 5, 3), loves: ['apple', 'watermelon'], weight: 650, gender: 'm', vampires: 54});
db.unicorns.insert({name: 'Nimue', dob: new Date(1999, 11, 20, 16, 15), loves: ['grape', 'carrot'], weight: 540, gender: 'f'});
db.unicorns.insert({name: 'Dunx', dob: new Date(1976, 6, 18, 18, 18), loves: ['grape', 'watermelon'], weight: 704, gender: 'm', vampires: 165});
P8
db.unicorns.update({weight: 590}, {$set: {name: 'Roooooodles', dob: new Date(1979, 7, 18, 18, 44), loves: ['apple'], gender: 'm', vampires: 99}})
db.unicorns.update({name: ’Roooooodles’}, {$set: {weight: 590}})
P9
db.unicorns.update({name: ’Aurora’}, {$push: {loves: ’sugar’}})
db.hits.update({page: ’unicorns’}, {$inc: {hits: 1}});
db.hits.find();
db.hits.update({page: ’unicorns’}, {$inc: {hits: 1}}, true);
<== enable upsert, 当记录不存在时自动创建(和save一样)
db.hits.find();
db.unicorns.update({}, {$set: {vaccinated: true }}, false, true);
<==upsert:false(default value), multiple updates:true 默认只更新第一条记录
P11
Field Selection:
db.unicorns.find(null, {name: 1});
<== 相当于select _id, name from unicorns
db.unicorns.find(null, {name: 1, _id: -1});
<== 相当于select name from unicorns
,注意1和-1不能同时指定,这里是唯一的例外
Ordering
db.unicorns.find().sort({name: 1, vampires: -1})
<==相当于order by name asc, vampires desc
Paging:
db.unicorns.find().sort({weight: -1}).limit(2).skip(1)
<==limit表示页大小pageSize (sort, limit, skip can be chained in any order)
Count
db.unicorns.count({vampires: {$gt: 50}})
<== 这种写法只是下面这种方式的shortcut
db.unicorns.find({vampires: {$gt: 50}}).count()
P13
db.employees.insert({_id: ObjectId('4d85c7039ab0fd70a117d730'), name: 'Leto'})
db.employees.insert({_id: ObjectId('4d85c7039ab0fd70a117d731'), name: 'Duncan', manager: ObjectId('4d85c7039ab0fd70a117d730')});
db.employees.insert({_id: ObjectId('4d85c7039ab0fd70a117d732'), name: 'Moneo', manager: ObjectId('4d85c7039ab0fd70a117d730')});
使用array来表达多对多或一对多关联:
db.employees.insert({_id: ObjectId(”4d85c7039ab0fd70a117d733”), name: ’Siona’, manager: [ObjectId
(”4d85c7039ab0fd70a117d730”), ObjectId(”4d85c7039ab0fd70a117d732”)] })
findByManager
db.employees.find({manager: ObjectId(”4d85c7039ab0fd70a117d730”)})
P14
db.employees.insert({_id: ObjectId('4d85c7039ab0fd70a117d734'), name: 'Ghanima', family: {mother:'Chani', father: 'Paul', brother: ObjectId('4d85c7039ab0fd70a117d730')}});
内嵌文档查询:
db.employees.find({'family.mother' : 'Chani'})
P18
capped collection
//limit our capped collection to 1 megabyte
db.createCollection(’logs’, {capped: true, size: 1048576})
使用capped collection,mongo可以自动清除旧文档.可以指定capped collection的大小(比如1M),也可以指定documents的最大数目.
详见: http://www.mongodb.org/display/DOCS/Capped+Collections
P24
db.hits.insert({resource: 'index', date: new Date(2010, 0, 20, 4, 30)});
db.hits.insert({resource: 'index', date: new Date(2010, 0, 20, 5, 30)});
db.hits.insert({resource: 'about', date: new Date(2010, 0, 20, 6, 0)});
db.hits.insert({resource: 'index', date: new Date(2010, 0, 20, 7, 0)});
db.hits.insert({resource: 'about', date: new Date(2010, 0, 21, 8, 0)});
db.hits.insert({resource: 'about', date: new Date(2010, 0, 21, 8, 30)});
db.hits.insert({resource: 'index', date: new Date(2010, 0, 21, 8, 30)});
db.hits.insert({resource: 'about', date: new Date(2010, 0, 21, 9, 0)});
db.hits.insert({resource: 'index', date: new Date(2010, 0, 21, 9, 30)});
db.hits.insert({resource: 'index', date: new Date(2010, 0, 22, 5, 0)});
MapReduce:
var map = function() {
var key = {resource: this.resource, year: this.date.getFullYear(), month: this.date.getMonth(), day: this.date.getDate()};
emit(key, {count: 1});
};
var reduce = function(key, values) {
var sum = 0;
values.forEach(function(value) {
sum += value[’count’];
});
return {count: sum};
};
db.hits.mapReduce(map, reduce, {out: {inline:1}})
db.hits.mapReduce(map, reduce, {out: ’hit_stats’});
db.hit_stats.find();
P28 备份:
mongodump.exe --db learn --out backup
备份的结果:
$ pwd
/c/bin/mongodb/bin/backup/learn
$ ls -lh
total 6.0k
-rw-r--r-- 1 cyper.yi Administ 265 May 19 15:12 employees.bson
-rw-r--r-- 1 cyper.yi Administ 97 May 19 15:12 employees.metadata.json
-rw-r--r-- 1 cyper.yi Administ 520 May 19 15:12 hit_stats.bson
-rw-r--r-- 1 cyper.yi Administ 97 May 19 15:12 hit_stats.metadata.json
-rw-r--r-- 1 cyper.yi Administ 560 May 19 15:12 hits.bson
-rw-r--r-- 1 cyper.yi Administ 92 May 19 15:12 hits.metadata.json
-rw-r--r-- 1 cyper.yi Administ 530 May 19 15:12 system.indexes.bson
-rw-r--r-- 1 cyper.yi Administ 53 May 19 15:12 test.bson
-rw-r--r-- 1 cyper.yi Administ 92 May 19 15:12 test.metadata.json
-rw-r--r-- 1 cyper.yi Administ 1.6k May 19 15:12 unicorns.bson
-rw-r--r-- 1 cyper.yi Administ 304 May 19 15:12 unicorns.metadata.json
还原:
mongorestore.exe --db learn -c unicorns backup/learn/unicorns.bson
书中的有误
$ mongorestore.exe --collection unicorns backup/learn/unicorns.bson
connected to: 127.0.0.1
assertion: 13106 nextSafe(): { $err: "Invalid ns [.unicorns]", code: 16256 }
备份成json格式及csv格式
cyper.yin@pc /c/bin/mongodb/bin
$ mongoexport --db learn --collection unicorns > hello.json
connected to: 127.0.0.1
exported 12 records
cyper.yin@pc /c/bin/mongodb/bin
$ mongoexport --db learn --collection unicorns --csv -fields name,weight,vampires > hello.csv
connected to: 127.0.0.1
exported 12 records
全书完,书中有不少外链,需要认真阅读.