MongoDB Quick Start

来自Jack's Lab
2014年12月16日 (二) 15:17Comcat (讨论 | 贡献)的版本

跳转到: 导航, 搜索

目录

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 in it.



2.2 Insert

Insert a doc (similar to line) into users (It's a Set, similar to table)

> use test
switched to db test
> show dbs
admin
local
> db.users.insert({username:"Jack Tan"})
> show dbs
admin
local
test



2.3 Delete DB

> show dbs
admin
local
test
> use test
switched to db test
> db.dropDatabase()
{ "dropped" : "test.$cmd", "ok" : 1 }
> show dbs
admin
local















个人工具
名字空间

变换
操作
导航
工具箱