This chapter is available in the official documentation at home-Quickstart.
Quick Start on OS X
Install MongoDB
The simplest way to install MongoDB is to use the Package Manager or pre-compiled binary files:
Package Manager
If you use homebrew Package Manager, run:
View plain
- $ Brew update
- $ Brew install MongoDB
If you use macports, you can install it as follows:
View plain
- $ Sudo port install MongoDB
The entire installation process takes a little time.
32-bit binary file
Note: We recommend that you use 64-bit (if you have a 64-bit operating system)
View plain
- $ Curl http://downloads.mongodb.org/osx/mongodb-osx-i386-x.y.z.tgz> Mongo. tgz
- $ Tar xzf Mongo. tgz
Replace x. y. Z with the version number of the current stable version.
64-bit binary file
View plain
- $ Curl http://downloads.mongodb.org/osx/mongodb-osx-x86_64-x.y.z.tgz> mongo. tgz
- $ Tar xzf mongo. tgz
Replace x. y. Z with the version number of the current stable version.
Create a data directory
By default, MongoDB stores data in/data/DB, but it does not automatically create this directory. Create as follows:
View plain
- $ Mkdir-p/data/db
You can also use the -- dbpath parameter to tell mongodb to use another data directory.
Run and connect to the server
First, start the mongodb server in a terminal:
View plain
- $./Mongodb-xxxxxxx/bin/mongod
Start shell on another terminal and it will automatically connect to the local machine by default:
View plain
- $./Mongodb-xxxxxxx/bin/mongo
- > Db. foo. save ({a: 1 })
- > Db. foo. find ()
Congratulations, you just used mongodb to store and retrieve your first document.
Quick Start in Linux
Download
Use the Package Manager
For Ubuntu and Debian users, see "Ubuntu and Debian packages ".
For CentOS and Fedora users, see "CentOS and Fedora Packages ".
32-bit linux binary file
Note: 64-bit is recommended.
View plain
- $ # Replace "1.6.4" in the url below with the version you want
- $ Curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.6.4.tgz> mongo. tgz
- $ Tar xzf mongo. tgz
64-bit Linux binary file
View plain
- $ # Replace "1.6.4" in the URL below with the version you want
- $ Curl http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.6.4.tgz> Mongo. tgz
- $ Tar xzf Mongo. tgz
Other Unixes
See "Download" and "generate ".
Create a data directory
By default, mongodb stores data in/data/db, but it does not automatically create this directory. In this way, create:
View plain
- $ Sudo mkdir-P/data/DB/
- $ Sudo chown 'id-U'/data/DB
You can also use the -- dbpath parameter to tell mongodb to use another data directory.
Run and connect to the server
First, start the mongodb server in a terminal:
View plain
- $./MongoDB-xxxxxxx/bin/mongod
Start shell on another terminal and it will automatically connect to the local machine by default:
View plain
- $./MongoDB-xxxxxxx/bin/Mongo
- > DB. Foo. Save ({A: 1 })
- > DB. Foo. Find ()
Congratulations, you just used mongodb to store and retrieve your first document.
Quick Start in Windows
Download
The simplest (recommended) method to install mongodb is to use the generated binary file. Note: We recommend that you have 64-bit windows.
32-bit binary file
Download and decompress the 32-bit compressed package. We recommend the "production" version.
64-bit binary file
Download and decompress the 64-bit compressed package.
Extract
Decompress the downloaded package to the local directory. For ease, you can also rename mongo-xxxxxx to mongo.
Create a data directory
By default, MongoDB stores data to \ data \ dB, but it does not automatically create this folder. You can create it as follows:
View plain
- C: \> mkdir \ data
- C: \> mkdir \ data \ db
Of course, you can also create this directory in the System Browser.
You can also use the -- dbpath parameter to tell MongoDB to use another data directory.
Run and connect to the server
Important binary files are:
- Mongod.exe-database server. You can use mongod -- help to get more detailed startup help.
- Cmd.exe-manage shell.
In the browser, double-click "cmd.exe" or start it in the command line:
View plain
- C: \> cd \ my_1__dir \ bin
- C: \ my_1__dir \ bin> mongod
Note: The server can also be started as a service.
Now, you can double-click cmd.exe or use the command line to start the Shell management. In bad condition, cmd.exe connects to the local server and uses a database named "test. Run Mongo -- help to obtain more information.
View plain
- C: \> cd \ my_1__dir \ bin
- C: \ my_1__dir \ bin> mongo
- > // The mongo shell is a javascript shell connected to the db
- > // By default it connects to database 'test' at localhost
- > 3 + 3
- 6
- > Db
- Test
- > // The first write will create the db:
- > Db. foo. insert ({a: 1 })
- > Db. foo. find ()
- {_ Id:..., a: 1}
- > Show dbs
- ...
- > Show collections
- ...
- > Help
Congratulations, you just stored and retrieved your first document through mongod.