MongoDB Quick Start

来自Jack's Lab
跳转到: 导航, 搜索

目录

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
}










个人工具
名字空间

变换
操作
导航
工具箱