Running MongoDB
The following procedure describes how to run, configure, start, log in to, and verify the MongoDB database.
- View the MongoDB version.
1 2 3
cd ~ find / -name mongod /usr/local/mongo/bin/mongod --version

- Modify the MongoDB configuration file.
- Delete the /etc/mongodb.cnf file and re-create it.
1 2
rm -f /etc/mongodb.cnf vim /etc/mongodb.cnf
- Press i to enter the insert mode and add the following content to the file:
1 2 3 4 5 6 7
dbpath=/data/mongo logpath=/data/mongo/mongo.log logappend=true port=27017 fork=true auth=false bind_ip=0.0.0.0
Parameters in the configuration file:
- dbpath: Directory for storing data files.
- logpath: Directory for storing log files.
- logappend=true: Indicates that logs are added in appending mode.
- port: Port number.
- fork=true: Enables it as a daemon, that is, runs it in the background.
- auth=false: Indicates that the user name and password do not need to be verified when connecting to the database.
- bind_ip: Addresses that are granted with the access permission. 127.0.0.1 indicates that only the login user has the access permission, whereas 0.0.0.0 indicates that all users have the access permission.
- Press Esc, type :wq!, and press Enter to save the file and exit.
- Delete the /etc/mongodb.cnf file and re-create it.
- Start the MongoDB database.
- Use the configuration file to start the MongoDB database.
1nohup /usr/local/mongo/bin/mongod -f /etc/mongodb.cnf &

- Check whether the MongoDB database process is started properly.
1ps -ef | grep mongod
In the command output, the database process ID is 36249 and the process has been started.

- View the MongoDB database listening port.
1netstat -anptIn this example, the listening port is 27017.

- Use the configuration file to start the MongoDB database.
- Log in to the MongoDB database and check whether the database is running properly.
- Log in to the MongoDB database from the local server.
1 2
cd /usr/local/mongo/bin ./mongo

View the login parameters:
1 2
/usr/local/mongo/bin/mongo --h /usr/local/mongo/bin/mongo --help
- Query the database information.
1show dbs
- Create a database.
1 2
use mongotest-database show dbs

- Run the use DATABASE_NAME command to create a database. If no database exists, a database will be created; otherwise, you will be switched to the specified database.
- The newly created database mongotest-database is not on the database list. To display the new database, you need to insert some data into mongotest-database.
- Create a collection.
1 2 3
use mongotest-database show collections db.createCollection("mongo_test")

To view existing collections, run the show collections or show tables command.
1 2
show collections show tables

- Insert a document.
1 2 3 4 5 6 7
db.mongo_test.insert({title: 'MongoDB tutorial', ... description: 'MongoDB is a NoSQL database.', ... by: 'Newbie tutorial', ... url: 'http://www.runoob.com', ... tags: ['mongodb', 'database', 'NoSQL'], ... likes: 100 ... })
In the preceding example, mongo_test is the collection name. If this collection does not exist in the database, MongoDB automatically creates the collection and inserts the document.
View the inserted document.
1db.mongo_test.find()
- Update the document.
Use the update () method to update the document title.
1 2
db.mongo_test.update({'title':'MongoDB tutorial'},{$set:{'title':'MongoDB'}}) db.mongo_test.find()

- Delete the document whose title is MongoDB.
1 2
db.mongo_test.remove({'title':'MongoDB'}) db.mongo_test.find()

- View indexes and create one.
1 2 3
db.mongo_test.getIndexes() db.mongo_test.createIndex({"title_test":1}) db.mongo_test.getIndexes()

- Delete the index title_test_1 created in 4.h.
1db.mongo_test.dropIndex("title_test_1")

- Delete the collection.
1 2 3
use mongotest-database show collections db.mongo_test.drop()

- Delete the database.
1 2 3
use mongotest-database show dbs db.dropDatabase()

- Log in to the MongoDB database from the local server.
- Exit the database.
1exit
Parent topic: Running