MongoDB Quick Start
来自Jack's Lab
(版本间的差异)
(→Mongo Shell) |
(→Index) |
||
(未显示1个用户的33个中间版本) | |||
第95行: | 第95行: | ||
</source> | </source> | ||
− | This can not create the database immediatelly. System will create it when there is data in | + | This can not create the database immediatelly. System will create it when there is data inserted in. |
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Create Collection === | ||
+ | |||
+ | System will create the collection when there is data inserted in: | ||
+ | |||
+ | <source lang=bash> | ||
+ | > use test | ||
+ | switched to db test | ||
+ | > db.getCollectionNames() | ||
+ | [ ] | ||
+ | > db.users.insert({username:"smith"}) | ||
+ | |||
+ | > show dbs | ||
+ | admin | ||
+ | local | ||
+ | test | ||
+ | |||
+ | > db.getCollectionNames() | ||
+ | [ "system.indexes", "users" ] | ||
+ | </source> | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Delete Collection === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > use test | ||
+ | switched to db test | ||
+ | > db.getCollectionNames() | ||
+ | [ "system.indexes", "users" ] | ||
+ | > db.users.drop() | ||
+ | true | ||
+ | > db.getCollectionNames() | ||
+ | [ "system.indexes" ] | ||
+ | </source> | ||
<br><br> | <br><br> | ||
第101行: | 第138行: | ||
=== Insert === | === Insert === | ||
− | Insert a doc (similar to line) into users (It's a | + | Insert a doc (similar to line) into users (It's a Collection, similar to table) |
<source lang=bash> | <source lang=bash> | ||
第110行: | 第147行: | ||
local | local | ||
> db.users.insert({username:"Jack Tan"}) | > db.users.insert({username:"Jack Tan"}) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fdcedfddefec3659e6e29"), "username" : "Jack Tan" } | ||
+ | |||
> show dbs | > show dbs | ||
admin | admin | ||
local | local | ||
test | test | ||
+ | |||
+ | > db.users.save({username:"Jones"}) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fdcedfddefec3659e6e29"), "username" : "Jack Tan" } | ||
+ | { "_id" : ObjectId("548fdd29fddefec3659e6e2a"), "username" : "Jones" } | ||
+ | > db.users.count() | ||
+ | 2 | ||
</source> | </source> | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Update === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "username" : "smith" } | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "username" : "Jack Tan" } | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | |||
+ | > db.users.update({username:"smith"},{$set:{country:"Canada"}}) | ||
+ | > db.users.find({username:"smith"}) | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "country" : "Canada", "username" : "smith" } | ||
+ | |||
+ | > db.users.update({username:"smith"},{$unset:{country: 1}}) | ||
+ | > db.users.find({username:"smith"}) | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "username" : "smith" } | ||
+ | |||
+ | > db.users.update({username:"smith"},{$set:{favorites: | ||
+ | ... { | ||
+ | ... cities:["Chicago", "Cheyenne"], | ||
+ | ... movies:["Casablanca", "The Sting"] | ||
+ | ... } | ||
+ | ... } | ||
+ | ... }) | ||
+ | > db.users.find({username:"smith"}) | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "cities" : [ "Chicago", "Cheyenne" ], | ||
+ | "movies" : [ "Casablanca", "The Sting" ] }, "username" : "smith" } | ||
+ | |||
+ | > db.users.update({username:"smith"},{$set:{favorites: | ||
+ | ... { | ||
+ | ... movies: ["Casablanca", "Rocky"] | ||
+ | ... } | ||
+ | ... } | ||
+ | ... }) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "username" : "Jack Tan" } | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } | ||
+ | </source> | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Multi-update === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca" ] }, "username" : "Jack Tan" } | ||
+ | > | ||
+ | > db.users.update({"favorites.movies":"Casablanca"},{$addToSet:{"favorites.movies":"The Maltese Falcon"}}, false, false) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky", "The Maltese Falcon" ] }, | ||
+ | "username" : "smith" } | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca" ] }, "username" : "Jack Tan" } | ||
+ | > | ||
+ | > | ||
+ | > db.users.update({username:"smith"},{$set:{favorites:{movies:["Casablanca","Rocky"]}}}) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca" ] }, "username" : "Jack Tan" } | ||
+ | > | ||
+ | > db.users.update({"favorites.movies":"Casablanca"},{$addToSet:{"favorites.movies":"The Maltese Falcon"}}, false, true) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky", "The Maltese Falcon" ] }, | ||
+ | "username" : "smith" } | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca", "The Maltese Falcon" ] }, | ||
+ | "username" : "Jack Tan" } | ||
+ | </source> | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Find === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.users.find({"favorites.movies":"Casablanca"}) | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } | ||
+ | |||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "username" : "Jack Tan" } | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } | ||
+ | |||
+ | > db.numbers.find({num:{"$gt":199997}}) | ||
+ | { "_id" : ObjectId("548ff0b1991680933fb8ad49"), "num" : 199998 } | ||
+ | { "_id" : ObjectId("548ff0b1991680933fb8ad4a"), "num" : 199999 } | ||
+ | |||
+ | > db.numbers.find({num:{"$gt":33,"$lt":36}}) | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02d"), "num" : 34 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02e"), "num" : 35 } | ||
+ | </source> | ||
+ | |||
+ | <br><br> | ||
+ | |||
+ | === Delete === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky", "The Maltese Falcon" ] }, | ||
+ | "username" : "smith" } | ||
+ | { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca", "The Maltese Falcon" ] }, | ||
+ | "username" : "Jack Tan" } | ||
+ | > db.users.remove({"favorites.movies":"Casablanca"}) | ||
+ | > db.users.find() | ||
+ | { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } | ||
+ | > db.users.count() | ||
+ | 1 | ||
+ | > db.users.remove() | ||
+ | > db.users.find() | ||
+ | > show collections | ||
+ | system.indexes | ||
+ | users | ||
+ | > db.users.count() | ||
+ | 0 | ||
+ | </source> | ||
+ | |||
<br><br> | <br><br> | ||
第133行: | 第302行: | ||
local | local | ||
</source> | </source> | ||
+ | |||
<br><br> | <br><br> | ||
+ | |||
+ | == Index == | ||
+ | |||
+ | <source lang=bash> | ||
+ | > for (i=0; i<200000; i++){ | ||
+ | ... db.numbers.save({num:i}); | ||
+ | ... } | ||
+ | > db.numbers.count() | ||
+ | 200000 | ||
+ | > db.numbers.find() | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a00b"), "num" : 0 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a00c"), "num" : 1 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a00d"), "num" : 2 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a00e"), "num" : 3 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a00f"), "num" : 4 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a010"), "num" : 5 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a011"), "num" : 6 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a012"), "num" : 7 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a013"), "num" : 8 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a014"), "num" : 9 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a015"), "num" : 10 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a016"), "num" : 11 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a017"), "num" : 12 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a018"), "num" : 13 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a019"), "num" : 14 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a01a"), "num" : 15 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a01b"), "num" : 16 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a01c"), "num" : 17 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a01d"), "num" : 18 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a01e"), "num" : 19 } | ||
+ | has more | ||
+ | > it | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a01f"), "num" : 20 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a020"), "num" : 21 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a021"), "num" : 22 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a022"), "num" : 23 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a023"), "num" : 24 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a024"), "num" : 25 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a025"), "num" : 26 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a026"), "num" : 27 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a027"), "num" : 28 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a028"), "num" : 29 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a029"), "num" : 30 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02a"), "num" : 31 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02b"), "num" : 32 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02c"), "num" : 33 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02d"), "num" : 34 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02e"), "num" : 35 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02f"), "num" : 36 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a030"), "num" : 37 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a031"), "num" : 38 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a032"), "num" : 39 } | ||
+ | has more | ||
+ | > db.numbers.find({num:500}) | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a1ff"), "num" : 500 } | ||
+ | > db.numbers.find({num:{"$gt":199997}}) | ||
+ | { "_id" : ObjectId("548ff0b1991680933fb8ad49"), "num" : 199998 } | ||
+ | { "_id" : ObjectId("548ff0b1991680933fb8ad4a"), "num" : 199999 } | ||
+ | |||
+ | > db.numbers.find({num:{"$gt":33,"$lt":36}}) | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02d"), "num" : 34 } | ||
+ | { "_id" : ObjectId("548ff09c991680933fb5a02e"), "num" : 35 } | ||
+ | |||
+ | > db.numbers.find({num:{"$gt":199997}}).explain() | ||
+ | { | ||
+ | "cursor" : "BasicCursor", | ||
+ | "indexBounds" : [ ], | ||
+ | "nscanned" : 200000, | ||
+ | "nscannedObjects" : 200000, | ||
+ | "n" : 2, | ||
+ | "millis" : 60, | ||
+ | "oldPlan" : { | ||
+ | "cursor" : "BasicCursor", | ||
+ | "indexBounds" : [ ] | ||
+ | }, | ||
+ | "allPlans" : [ | ||
+ | { | ||
+ | "cursor" : "BasicCursor", | ||
+ | "indexBounds" : [ ] | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Find a line, the time is 60ms | ||
+ | |||
+ | |||
+ | Use Indexes: | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.numbers.ensureIndex({num:1}) | ||
+ | > db.numbers.getIndexes() | ||
+ | [ | ||
+ | { | ||
+ | "name" : "_id_", | ||
+ | "ns" : "test.numbers", | ||
+ | "key" : { | ||
+ | "_id" : 1 | ||
+ | } | ||
+ | }, | ||
+ | { | ||
+ | "_id" : ObjectId("548ff1ea991680933fb8ad4b"), | ||
+ | "ns" : "test.numbers", | ||
+ | "key" : { | ||
+ | "num" : 1 | ||
+ | }, | ||
+ | "name" : "num_1" | ||
+ | } | ||
+ | ] | ||
+ | > db.numbers.find({num:{"$gt":199997}}).explain() | ||
+ | { | ||
+ | "cursor" : "BtreeCursor num_1", | ||
+ | "indexBounds" : [ | ||
+ | [ | ||
+ | { | ||
+ | "num" : 199997 | ||
+ | }, | ||
+ | { | ||
+ | "num" : 1.7976931348623157e+308 | ||
+ | } | ||
+ | ] | ||
+ | ], | ||
+ | "nscanned" : 3, | ||
+ | "nscannedObjects" : 2, | ||
+ | "n" : 2, | ||
+ | "millis" : 0, | ||
+ | "allPlans" : [ | ||
+ | { | ||
+ | "cursor" : "BtreeCursor num_1", | ||
+ | "indexBounds" : [ | ||
+ | [ | ||
+ | { | ||
+ | "num" : 199997 | ||
+ | }, | ||
+ | { | ||
+ | "num" : 1.7976931348623157e+308 | ||
+ | } | ||
+ | ] | ||
+ | ] | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | The time is less than 1ms | ||
+ | |||
<br><br> | <br><br> | ||
+ | |||
+ | == Basic Management == | ||
+ | |||
+ | === Show === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > show dbs | ||
+ | admin | ||
+ | local | ||
+ | test | ||
+ | > use test | ||
+ | switched to db test | ||
+ | |||
+ | > show collections | ||
+ | system.indexes | ||
+ | users | ||
+ | </source> | ||
+ | |||
<br><br> | <br><br> | ||
+ | |||
+ | === Stats === | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.stats() | ||
+ | { | ||
+ | "collections" : 2, | ||
+ | "objects" : 1, | ||
+ | "dataSize" : 60, | ||
+ | "storageSize" : 6144, | ||
+ | "numExtents" : 2, | ||
+ | "indexes" : 1, | ||
+ | "indexSize" : 8192, | ||
+ | "ok" : 1 | ||
+ | } | ||
+ | > db.users.stats() | ||
+ | { | ||
+ | "ns" : "test.users", | ||
+ | "count" : 0, | ||
+ | "size" : 0, | ||
+ | "storageSize" : 2560, | ||
+ | "numExtents" : 1, | ||
+ | "nindexes" : 1, | ||
+ | "lastExtentSize" : 2560, | ||
+ | "paddingFactor" : 1.49, | ||
+ | "flags" : 1, | ||
+ | "totalIndexSize" : 8192, | ||
+ | "indexSizes" : { | ||
+ | "_id_" : 8192 | ||
+ | }, | ||
+ | "ok" : 1 | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | The true is: | ||
+ | |||
+ | <source lang=bash> | ||
+ | > db.runCommand | ||
+ | function (obj) { | ||
+ | if (typeof obj == "string") { | ||
+ | var n = {}; | ||
+ | n[obj] = 1; | ||
+ | obj = n; | ||
+ | } | ||
+ | return this.getCollection("$cmd").findOne(obj); | ||
+ | } | ||
+ | > db.runCommand({dbstats:1}) | ||
+ | { | ||
+ | "collections" : 2, | ||
+ | "objects" : 1, | ||
+ | "dataSize" : 60, | ||
+ | "storageSize" : 6144, | ||
+ | "numExtents" : 2, | ||
+ | "indexes" : 1, | ||
+ | "indexSize" : 8192, | ||
+ | "ok" : 1 | ||
+ | } | ||
+ | > | ||
+ | > db.runCommand({dbstats:'numbers'}) | ||
+ | { | ||
+ | "collections" : 2, | ||
+ | "objects" : 1, | ||
+ | "dataSize" : 60, | ||
+ | "storageSize" : 6144, | ||
+ | "numExtents" : 2, | ||
+ | "indexes" : 1, | ||
+ | "indexSize" : 8192, | ||
+ | "ok" : 1 | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
<br><br> | <br><br> | ||
<br><br> | <br><br> | ||
<br><br> | <br><br> | ||
<br><br> | <br><br> |
2014年12月16日 (二) 16:52的最后版本
目录 |
[编辑] 1 Install
comcat@maike:~# apt-get install mongodb Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libboost-filesystem1.42.0 libboost-program-options1.42.0 libboost-system1.42.0 libboost-thread1.42.0 libmozjs2d libpcrecpp0 mongodb-clients mongodb-dev mongodb-server ...... ...... Processing triggers for man-db ... Setting up libboost-system1.42.0 (1.42.0-4) ... Setting up libboost-filesystem1.42.0 (1.42.0-4) ... Setting up libboost-program-options1.42.0 (1.42.0-4) ... Setting up libboost-thread1.42.0 (1.42.0-4) ... Setting up libmozjs2d (1.9.1.16-20) ... Setting up libpcrecpp0 (8.02-1.1) ... Setting up mongodb-clients (1:1.4.4-3) ... Setting up mongodb-server (1:1.4.4-3) ... Adding system user `mongodb' (UID 105) ... Adding new user `mongodb' (UID 105) with group `nogroup' ... Not creating home directory `/home/mongodb'. Adding group `mongodb' (GID 110) ... Done. Adding user mongodb to group mongodb Done. Starting database: mongodb. Setting up mongodb-dev (1:1.4.4-3) ... Setting up mongodb (1:1.4.4-3) ... comcat@maike:~# ps ax|grep mongo 30109 ? Sl 0:00 /usr/bin/mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongodb.log --config /etc/mongodb.conf run 30319 pts/0 S+ 0:00 grep mongo
The configuration file is located at /etc/mongodb.conf
It use /var/lib/mongodb as the database directory by default. You can modify it as you like.
[编辑] 2 Mongo Shell
comcat@maike:~# dpkg -L mongodb-clients | grep bin /usr/bin /usr/bin/mongo /usr/bin/mongodump /usr/bin/mongoexport /usr/bin/mongofiles /usr/bin/mongoimport /usr/bin/mongorestore /usr/bin/mongostat comcat@maike:~# mongo MongoDB shell version: 1.4.4 url: test connecting to: test type "help" for help > > help HELP show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms use <db name> set curent database to <db name> db.help() help on DB methods db.foo.help() help on collection methods db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate > show dbs admin local
[编辑] 2.1 Create DB
comcat@maike:~$ mongo MongoDB shell version: 1.4.4 url: test connecting to: test type "help" for help > > use test switched to db test > show dbs admin local >
This can not create the database immediatelly. System will create it when there is data inserted in.
[编辑] 2.2 Create Collection
System will create the collection when there is data inserted in:
> use test switched to db test > db.getCollectionNames() [ ] > db.users.insert({username:"smith"}) > show dbs admin local test > db.getCollectionNames() [ "system.indexes", "users" ]
[编辑] 2.3 Delete Collection
> use test switched to db test > db.getCollectionNames() [ "system.indexes", "users" ] > db.users.drop() true > db.getCollectionNames() [ "system.indexes" ]
[编辑] 2.4 Insert
Insert a doc (similar to line) into users (It's a Collection, similar to table)
> use test switched to db test > show dbs admin local > db.users.insert({username:"Jack Tan"}) > db.users.find() { "_id" : ObjectId("548fdcedfddefec3659e6e29"), "username" : "Jack Tan" } > show dbs admin local test > db.users.save({username:"Jones"}) > db.users.find() { "_id" : ObjectId("548fdcedfddefec3659e6e29"), "username" : "Jack Tan" } { "_id" : ObjectId("548fdd29fddefec3659e6e2a"), "username" : "Jones" } > db.users.count() 2
[编辑] 2.5 Update
> db.users.find() { "_id" : ObjectId("548fe214991680933fb5a008"), "username" : "smith" } { "_id" : ObjectId("548fe28e991680933fb5a009"), "username" : "Jack Tan" } { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } > db.users.update({username:"smith"},{$set:{country:"Canada"}}) > db.users.find({username:"smith"}) { "_id" : ObjectId("548fe214991680933fb5a008"), "country" : "Canada", "username" : "smith" } > db.users.update({username:"smith"},{$unset:{country: 1}}) > db.users.find({username:"smith"}) { "_id" : ObjectId("548fe214991680933fb5a008"), "username" : "smith" } > db.users.update({username:"smith"},{$set:{favorites: ... { ... cities:["Chicago", "Cheyenne"], ... movies:["Casablanca", "The Sting"] ... } ... } ... }) > db.users.find({username:"smith"}) { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "cities" : [ "Chicago", "Cheyenne" ], "movies" : [ "Casablanca", "The Sting" ] }, "username" : "smith" } > db.users.update({username:"smith"},{$set:{favorites: ... { ... movies: ["Casablanca", "Rocky"] ... } ... } ... }) > db.users.find() { "_id" : ObjectId("548fe28e991680933fb5a009"), "username" : "Jack Tan" } { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" }
[编辑] 2.6 Multi-update
> db.users.find() { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca" ] }, "username" : "Jack Tan" } > > db.users.update({"favorites.movies":"Casablanca"},{$addToSet:{"favorites.movies":"The Maltese Falcon"}}, false, false) > db.users.find() { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky", "The Maltese Falcon" ] }, "username" : "smith" } { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca" ] }, "username" : "Jack Tan" } > > > db.users.update({username:"smith"},{$set:{favorites:{movies:["Casablanca","Rocky"]}}}) > db.users.find() { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca" ] }, "username" : "Jack Tan" } > > db.users.update({"favorites.movies":"Casablanca"},{$addToSet:{"favorites.movies":"The Maltese Falcon"}}, false, true) > db.users.find() { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky", "The Maltese Falcon" ] }, "username" : "smith" } { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca", "The Maltese Falcon" ] }, "username" : "Jack Tan" }
[编辑] 2.7 Find
> db.users.find({"favorites.movies":"Casablanca"}) { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } > db.users.find() { "_id" : ObjectId("548fe28e991680933fb5a009"), "username" : "Jack Tan" } { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky" ] }, "username" : "smith" } > db.numbers.find({num:{"$gt":199997}}) { "_id" : ObjectId("548ff0b1991680933fb8ad49"), "num" : 199998 } { "_id" : ObjectId("548ff0b1991680933fb8ad4a"), "num" : 199999 } > db.numbers.find({num:{"$gt":33,"$lt":36}}) { "_id" : ObjectId("548ff09c991680933fb5a02d"), "num" : 34 } { "_id" : ObjectId("548ff09c991680933fb5a02e"), "num" : 35 }
[编辑] 2.8 Delete
> db.users.find() { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } { "_id" : ObjectId("548fe214991680933fb5a008"), "favorites" : { "movies" : [ "Casablanca", "Rocky", "The Maltese Falcon" ] }, "username" : "smith" } { "_id" : ObjectId("548fe28e991680933fb5a009"), "favorites" : { "movies" : [ "Casablanca", "The Maltese Falcon" ] }, "username" : "Jack Tan" } > db.users.remove({"favorites.movies":"Casablanca"}) > db.users.find() { "_id" : ObjectId("548fe29d991680933fb5a00a"), "username" : "Gaga Liu" } > db.users.count() 1 > db.users.remove() > db.users.find() > show collections system.indexes users > db.users.count() 0
[编辑] 2.9 Delete DB
> show dbs admin local test > use test switched to db test > db.dropDatabase() { "dropped" : "test.$cmd", "ok" : 1 } > show dbs admin local
[编辑] 3 Index
> for (i=0; i<200000; i++){ ... db.numbers.save({num:i}); ... } > db.numbers.count() 200000 > db.numbers.find() { "_id" : ObjectId("548ff09c991680933fb5a00b"), "num" : 0 } { "_id" : ObjectId("548ff09c991680933fb5a00c"), "num" : 1 } { "_id" : ObjectId("548ff09c991680933fb5a00d"), "num" : 2 } { "_id" : ObjectId("548ff09c991680933fb5a00e"), "num" : 3 } { "_id" : ObjectId("548ff09c991680933fb5a00f"), "num" : 4 } { "_id" : ObjectId("548ff09c991680933fb5a010"), "num" : 5 } { "_id" : ObjectId("548ff09c991680933fb5a011"), "num" : 6 } { "_id" : ObjectId("548ff09c991680933fb5a012"), "num" : 7 } { "_id" : ObjectId("548ff09c991680933fb5a013"), "num" : 8 } { "_id" : ObjectId("548ff09c991680933fb5a014"), "num" : 9 } { "_id" : ObjectId("548ff09c991680933fb5a015"), "num" : 10 } { "_id" : ObjectId("548ff09c991680933fb5a016"), "num" : 11 } { "_id" : ObjectId("548ff09c991680933fb5a017"), "num" : 12 } { "_id" : ObjectId("548ff09c991680933fb5a018"), "num" : 13 } { "_id" : ObjectId("548ff09c991680933fb5a019"), "num" : 14 } { "_id" : ObjectId("548ff09c991680933fb5a01a"), "num" : 15 } { "_id" : ObjectId("548ff09c991680933fb5a01b"), "num" : 16 } { "_id" : ObjectId("548ff09c991680933fb5a01c"), "num" : 17 } { "_id" : ObjectId("548ff09c991680933fb5a01d"), "num" : 18 } { "_id" : ObjectId("548ff09c991680933fb5a01e"), "num" : 19 } has more > it { "_id" : ObjectId("548ff09c991680933fb5a01f"), "num" : 20 } { "_id" : ObjectId("548ff09c991680933fb5a020"), "num" : 21 } { "_id" : ObjectId("548ff09c991680933fb5a021"), "num" : 22 } { "_id" : ObjectId("548ff09c991680933fb5a022"), "num" : 23 } { "_id" : ObjectId("548ff09c991680933fb5a023"), "num" : 24 } { "_id" : ObjectId("548ff09c991680933fb5a024"), "num" : 25 } { "_id" : ObjectId("548ff09c991680933fb5a025"), "num" : 26 } { "_id" : ObjectId("548ff09c991680933fb5a026"), "num" : 27 } { "_id" : ObjectId("548ff09c991680933fb5a027"), "num" : 28 } { "_id" : ObjectId("548ff09c991680933fb5a028"), "num" : 29 } { "_id" : ObjectId("548ff09c991680933fb5a029"), "num" : 30 } { "_id" : ObjectId("548ff09c991680933fb5a02a"), "num" : 31 } { "_id" : ObjectId("548ff09c991680933fb5a02b"), "num" : 32 } { "_id" : ObjectId("548ff09c991680933fb5a02c"), "num" : 33 } { "_id" : ObjectId("548ff09c991680933fb5a02d"), "num" : 34 } { "_id" : ObjectId("548ff09c991680933fb5a02e"), "num" : 35 } { "_id" : ObjectId("548ff09c991680933fb5a02f"), "num" : 36 } { "_id" : ObjectId("548ff09c991680933fb5a030"), "num" : 37 } { "_id" : ObjectId("548ff09c991680933fb5a031"), "num" : 38 } { "_id" : ObjectId("548ff09c991680933fb5a032"), "num" : 39 } has more > db.numbers.find({num:500}) { "_id" : ObjectId("548ff09c991680933fb5a1ff"), "num" : 500 } > db.numbers.find({num:{"$gt":199997}}) { "_id" : ObjectId("548ff0b1991680933fb8ad49"), "num" : 199998 } { "_id" : ObjectId("548ff0b1991680933fb8ad4a"), "num" : 199999 } > db.numbers.find({num:{"$gt":33,"$lt":36}}) { "_id" : ObjectId("548ff09c991680933fb5a02d"), "num" : 34 } { "_id" : ObjectId("548ff09c991680933fb5a02e"), "num" : 35 } > db.numbers.find({num:{"$gt":199997}}).explain() { "cursor" : "BasicCursor", "indexBounds" : [ ], "nscanned" : 200000, "nscannedObjects" : 200000, "n" : 2, "millis" : 60, "oldPlan" : { "cursor" : "BasicCursor", "indexBounds" : [ ] }, "allPlans" : [ { "cursor" : "BasicCursor", "indexBounds" : [ ] } ] }
Find a line, the time is 60ms
Use Indexes:
> db.numbers.ensureIndex({num:1}) > db.numbers.getIndexes() [ { "name" : "_id_", "ns" : "test.numbers", "key" : { "_id" : 1 } }, { "_id" : ObjectId("548ff1ea991680933fb8ad4b"), "ns" : "test.numbers", "key" : { "num" : 1 }, "name" : "num_1" } ] > db.numbers.find({num:{"$gt":199997}}).explain() { "cursor" : "BtreeCursor num_1", "indexBounds" : [ [ { "num" : 199997 }, { "num" : 1.7976931348623157e+308 } ] ], "nscanned" : 3, "nscannedObjects" : 2, "n" : 2, "millis" : 0, "allPlans" : [ { "cursor" : "BtreeCursor num_1", "indexBounds" : [ [ { "num" : 199997 }, { "num" : 1.7976931348623157e+308 } ] ] } ] }
The time is less than 1ms
[编辑] 4 Basic Management
[编辑] 4.1 Show
> show dbs admin local test > use test switched to db test > show collections system.indexes users
[编辑] 4.2 Stats
> db.stats() { "collections" : 2, "objects" : 1, "dataSize" : 60, "storageSize" : 6144, "numExtents" : 2, "indexes" : 1, "indexSize" : 8192, "ok" : 1 } > db.users.stats() { "ns" : "test.users", "count" : 0, "size" : 0, "storageSize" : 2560, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 2560, "paddingFactor" : 1.49, "flags" : 1, "totalIndexSize" : 8192, "indexSizes" : { "_id_" : 8192 }, "ok" : 1 }
The true is:
> db.runCommand function (obj) { if (typeof obj == "string") { var n = {}; n[obj] = 1; obj = n; } return this.getCollection("$cmd").findOne(obj); } > db.runCommand({dbstats:1}) { "collections" : 2, "objects" : 1, "dataSize" : 60, "storageSize" : 6144, "numExtents" : 2, "indexes" : 1, "indexSize" : 8192, "ok" : 1 } > > db.runCommand({dbstats:'numbers'}) { "collections" : 2, "objects" : 1, "dataSize" : 60, "storageSize" : 6144, "numExtents" : 2, "indexes" : 1, "indexSize" : 8192, "ok" : 1 }