MongoDB Quick Start
来自Jack's Lab
(版本间的差异)
(→Create Collection) |
(→Create Collection) |
||
第159行: | 第159行: | ||
<source lang=bash> | <source lang=bash> | ||
− | > use | + | > use test |
− | switched to db | + | switched to db test |
> db.getCollectionNames() | > db.getCollectionNames() | ||
[ ] | [ ] | ||
− | > db. | + | > db.users.save({username:"smith"}) |
> db.getCollectionNames() | > db.getCollectionNames() | ||
− | [ "system.indexes", " | + | [ "system.indexes", "users" ] |
</source> | </source> | ||
2014年12月16日 (二) 15:35的版本
目录 |
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 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.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
2.4 Update
>
2.5 Create Collection
System will create the collection when there is data inserted in:
> use test switched to db test > db.getCollectionNames() [ ] > db.users.save({username:"smith"}) > db.getCollectionNames() [ "system.indexes", "users" ]
2.6 Delete Collection
> use test switched to db test > db.getCollectionNames() [ "system.indexes", "user", "users" ] > db.user.drop() true > db.getCollectionNames() [ "system.indexes", "users" ]