Databases Reference
In-Depth Information
Starting MongoDB and Storing Data
Assuming you have installed MongoDB successfully, start the server and connect to it.
You can start a MongoDB server by running the
mongod
program within the
bin
folder of the
distribution. Distributions vary according to the underlying environment, which can be Windows,
Mac OS X, or a Linux variant, but in each case the server program has the same name and it resides
in a folder named
bin
in the distribution.
The simplest way to connect to the MongoDB server is to use the JavaScript shell available with
the distribution. Simply run
mongo
from your command-line interface. The
mongo
JavaScript shell
command is also found in the
bin
folder.
When you start the MongoDB server by running
mongod
, you should see output on your console
that looks similar to the following:
PS C:\applications\mongodb-win32-x86_64-1.8.1> .\bin\mongod.exe
C:\applications\mongodb-win32-x86_64-1.8.1\bin\mongod.exe
--help for help and startup options
Sun May 01 21:22:56 [initandlisten] MongoDB starting : pid=3300 port=27017
dbpath=/data/db/ 64-bit
Sun May 01 21:22:56 [initandlisten] db version v1.8.1, pdfile version 4.5
Sun May 01 21:22:56 [initandlisten] git version:
a429cd4f535b2499cc4130b06ff7c26f41c00f04
Sun May 01 21:22:56 [initandlisten] build sys info: windows (6, 1, 7600, 2, '')
BOOST_LIB_VERSION=1_42
Sun May 01 21:22:56 [initandlisten] waiting for connections on port 27017
Sun May 01 21:22:56 [websvr] web admin interface listening on port 28017
This particular output was captured on a Windows 7 64-bit machine when
mongod
was run via the
Windows PowerShell. Depending on your environment your output may vary.
Now that the database server is up and running, use the
mongo
JavaScript shell to connect to it. The
initial output of the shell should be as follows:
PS C:\applications\mongodb-win32-x86_64-1.8.1> bin/mongo
MongoDB shell version: 1.8.1
connecting to: test
>
By default, the
mongo
shell connects to the “test” database available on localhost. From
mongod
(the server daemon program) console output, you can also guess that the MongoDB server waits
for connections on port 27017. To explore a possible set of initial commands just type
help
on the
mongo
interactive console. On typing
help
and pressing the Enter (or Return) key, you should see a
list of command options like so:
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
rs.help() help on replica set methods