Rate This Document
Findability
Accuracy
Completeness
Readability

Running MongoDB

The following procedure describes how to run, configure, start, log in to, and verify the MongoDB database.

  1. View the MongoDB version.
    1
    2
    3
    cd ~
    find / -name mongod
    /usr/local/mongo/bin/mongod --version
    

  2. Modify the MongoDB configuration file.
    1. Delete the /etc/mongodb.cnf file and re-create it.
      1
      2
      rm -f /etc/mongodb.cnf
      vim /etc/mongodb.cnf
      
    2. 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.
    3. Press Esc, type :wq!, and press Enter to save the file and exit.
  3. Start the MongoDB database.
    1. Use the configuration file to start the MongoDB database.
      1
      nohup /usr/local/mongo/bin/mongod -f /etc/mongodb.cnf &
      

    2. Check whether the MongoDB database process is started properly.
      1
      ps -ef | grep mongod
      

      In the command output, the database process ID is 36249 and the process has been started.

    3. View the MongoDB database listening port.
      1
      netstat -anpt
      

      In this example, the listening port is 27017.

  4. Log in to the MongoDB database and check whether the database is running properly.
    1. 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
      
    2. Query the database information.
      1
      show dbs
      

    3. 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.
    4. 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
      

    5. 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.

      1
      db.mongo_test.find()
      

    6. 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()
      

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

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

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

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

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

  5. Exit the database.
    1
    exit