Latest web development tutorials

Linux platform installation MongoDB

MongoDB provides a platform on linux 32-bit and 64-bit installation package, you can download the installation package in the official website.

Download: http://www.mongodb.org/downloads

After downloading the installer package, and extract tgz (The following demonstration is to install Linux on 64-bit).

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz    # 下载
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz                                   # 解压

mv  mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb                         # 将解压包拷贝到指定目录

MongoDB executable file located in the bin directory, so you can add it to the PATH:

export PATH=<mongodb-install-directory>/bin:$PATH

<mongodb-install-directory> MongoDB for your installation path. As this article / usr / local / mongodb.



Create a database directory

MongoDB data stored in the db directory data directory, but this directory is not automatically created during the installation process, so you need to manually create the data directory, and create db directory data directory.

The following example we will create a data directory in the root directory (/).

Note: / data / db MongoDB database path is the default startup (--dbpath).

mkdir -p /data/db


The command line to run MongoDB service

You can then execute the command line mongo installation directory bin directory to start the command execution mongod mongdb service.

Note: If your database directory is not / data / db, can be specified by --dbpath.

$ ./mongod
2015-09-25T16:39:50.549+0800 I JOURNAL  [initandlisten] journal dir=/data/db/journal
2015-09-25T16:39:50.550+0800 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-09-25T16:39:50.869+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 3.16
2015-09-25T16:39:51.206+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 3.52
2015-09-25T16:39:52.775+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 7.7


MongoDB Manage Shell

If you need to enter the MongoDB management background, you need to open the bin directory mongodb installation directory, and then execute mongo command file.

MongoDB MongoDB Shell is carrying interactive Javascript shell, used to operate and manage MongoDB interactive environment.

When you enter mongoDB the background, it will default to link to test document (database):

$ cd /usr/local/mongodb/bin
$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
Welcome to the MongoDB shell.
……

Since it is a JavaScript shell, you can run some simple arithmetic:

> 2+2
4
> 3+6
9

Now let's insert some simple data, retrieve and insert data:

> db.w3big.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.w3big.find()
{ "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 }
>

The first command is inserted into a digital x 10 field w3big collection.



MongoDb web user interface

MongoDB provides a simple HTTP user interface. If you want to enable this feature, you need to specify the parameters --rest at boot time.

$ ./mongod --dbpath=/data/db --rest

MongoDB Web interface to access the service port than port 1000.

If you use the default MongoDB port 27017 to run, you can access the web user interface to 28017 in the port number that the address is: http: // localhost: 28017.