Home | Mirror | Search |
目錄
http://www.mongodb.org/
Install MongoDB
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.5.5.tgz debian:/srv# tar zxf mongodb-linux-x86_64-1.5.5.tgz debian:/srv# ln -s mongodb-linux-x86_64-1.5.5 mongodb
Create a data directory
By default MongoDB will store data in /data/db, but it won't automatically create that directory. To create it, do:
$ sudo mkdir -p /data/db/ $ sudo chown `id -u` /data/db
You can also tell MongoDB to use a different data directory, with the --dbpath option.
Run and connect to the server
First, start the MongoDB server in one terminal:
$ ./mongodb/bin/mongod
In a separate terminal, start the shell, which will connect to localhost by default:
$ ./mongodb/bin/mongo > db.foo.save( { a : 1 } ) > db.foo.find()
Congratulations, you've just saved and retrieved your first document with MongoDB!
例 1.1. MongoDB Test
debian:/srv/mongodb/bin# ./mongo MongoDB shell version: 1.5.5 connecting to: test [initandlisten] Thu Jul 22 16:42:07 connection accepted from 127.0.0.1:42876 #1 > db.foo.save({a:1}) Thu Jul 22 16:42:23 allocating new datafile /data/db/test.ns, filling with zeroes... Thu Jul 22 16:42:23 done allocating datafile /data/db/test.ns, size: 16MB, took 0.025 secs Thu Jul 22 16:42:23 allocating new datafile /data/db/test.0, filling with zeroes... Thu Jul 22 16:42:23 done allocating datafile /data/db/test.0, size: 64MB, took 0.105 secs [conn1] Thu Jul 22 16:42:23 building new index on { _id: 1 } for test.foo [conn1] Thu Jul 22 16:42:23 Buildindex test.foo idxNo:0 { name: "_id_", ns: "test.foo", key: { _id: 1 } } [conn1] Thu Jul 22 16:42:23 done for 0 records 0secs [conn1] Thu Jul 22 16:42:23 insert test.foo 136ms > Thu Jul 22 16:42:23 allocating new datafile /data/db/test.1, filling with zeroes... Thu Jul 22 16:42:24 done allocating datafile /data/db/test.1, size: 128MB, took 0.228 secs > db.foo.find() { "_id" : ObjectId("4c48046f74050cbf6c9a0ef6"), "a" : 1 } > use neo switched to db neo > db.foo.save({a:1}) Thu Jul 22 16:54:50 allocating new datafile /data/db/neo.ns, filling with zeroes... Thu Jul 22 16:54:50 done allocating datafile /data/db/neo.ns, size: 16MB, took 0.026 secs Thu Jul 22 16:54:50 allocating new datafile /data/db/neo.0, filling with zeroes... Thu Jul 22 16:54:50 done allocating datafile /data/db/neo.0, size: 64MB, took 0.122 secs [conn1] Thu Jul 22 16:54:50 building new index on { _id: 1 } for neo.foo [conn1] Thu Jul 22 16:54:50 Buildindex neo.foo idxNo:0 { name: "_id_", ns: "neo.foo", key: { _id: 1 } } Thu Jul 22 16:54:50 allocating new datafile /data/db/neo.1, filling with zeroes... [conn1] Thu Jul 22 16:54:50 done for 0 records 0.01secs [conn1] Thu Jul 22 16:54:50 insert neo.foo 164ms > Thu Jul 22 16:54:50 done allocating datafile /data/db/neo.1, size: 128MB, took 0.217 secs > db.foo.find() { "_id" : ObjectId("4c48075a74050cbf6c9a0ef7"), "a" : 1 } > > db.neo.save({a:1}) [conn1] Thu Jul 22 16:58:32 building new index on { _id: 1 } for neo.neo [conn1] Thu Jul 22 16:58:32 Buildindex neo.neo idxNo:0 { name: "_id_", ns: "neo.neo", key: { _id: 1 } } [conn1] Thu Jul 22 16:58:32 done for 0 records 0.024secs > db.neo.find() { "_id" : ObjectId("4c48083874050cbf6c9a0ef8"), "a" : 1 }
$ sudo apt-get install mongodb-server mongodb-clients
$ /etc/init.d/mongodb Usage: /etc/init.d/mongodb {start|stop|force-stop|restart|force-reload|status}