Basic MongoDB Usage

Differences between Oracle and MongoDB

MongoDB is a database commonly called NoSQL. It stores data called documents, which are similar to JSON, in groups called collections. Using Excel and Oracle as familiar relational database examples, the corresponding names in MongoDB are as follows.

Excel Oracle MongoDB
Book Schema Database
Sheet Table Collection
Row Row Document
Column Column Field
Shell Field Value

Server

The server command is mongod. This section explains the commands for starting and stopping the server with mongod. For mongod options not covered here, see “How to use the MongoDB mongod command.”

Starting and stopping the server

  • Start the server
    • Assume that the location for storing database files is prepared at C:\mongodb\server\data.
    • Start Command Prompt and run the following command to start the MongoDB server.
      mongod --dbpath "C:\mongodb\server\data"
      
  • Stop the server
    • Press Ctrl+C in the running Command Prompt. A shutdown command is issued and MongoDB exits.

Client

After starting MongoDB in the previous step, connect with the client command mongo. If you are connecting from the same server as the MongoDB server, start a separate Command Prompt from the one used to start the server. For other mongo options not covered here, see “How to use the MongoDB mongo command.”

Connecting to and disconnecting from the server

  • Connect to the server
    mongo
    
    If you are on the same machine where MongoDB is running, start Command Prompt and execute the command above to connect. If the server is on another host, specify the target with the --host <hostname> option.
  • Disconnect from the server
    To disconnect from MongoDB, press Ctrl+C. You can also disconnect by running the following command.
    > exit
    

Creating, listing, changing, and deleting databases

  • Create a database

    > use sample_database
    

    Connects to the specified database. If the database does not exist, it is created.

  • List databases

    > show dbs
    

    By default, the three databases admin, local, and test exist.

    A database created immediately after running use <database name> is not shown in the list. It must contain at least one record before it appears in show dbs.

  • Rename a database

    > db.copyDatabase('old_database', 'new_database')
    > use old_database
    > db.dropDatabase()
    

    Unfortunately, there is no command to rename a database, so you must copy the old database to a new database and then delete the old one.

  • Delete a database

    > use sample_database
    > db.dropDatabase();
    

    Connect to the database to delete, then run db.dropDatabase().

Creating, listing, renaming, and deleting collections

  • Create a collection

    > db.createCollection(products);
    

    Creates a collection with the name specified in the argument.

  • List collections

    > show collections
    

    Displays the list of collections in the currently connected database.

  • Rename a collection

    > db.old_collection_name.renameCollection(new_collection_name, true);
    

    Renames old_collection_name to new_collection_name. If you want to drop an existing collection named old_collection_name during the rename, specify true as the second argument.

  • Delete a collection

    > db.products.drop();
    

    Deletes the specified collection.

Adding, querying, updating, and deleting documents

  • Add a document

    > db.products.insert(
            {name: 'sample book', price: 1280}
        )
    

    Adds a document to the specified collection. When insert is executed, the _id field is added automatically.

  • Query documents

    > db.products.find(
             {name: {$eq: 'sample book'}}
        )
    

    The basic format of a query argument is { field: { operator: value }}. Operators are documented in MongoDB - Query and Projection Operators. If no argument is specified, all data is returned.

  • Update documents

    > db.products.update(
            {name: {$eq: 'sample book'}},
            {$set: {name: 'Hack MongoDB'}},
            {upsert: false, multi: true}
        )
    

    Updates documents that match the condition specified in the first argument with the document content specified in the second argument. In the third argument object, upsert specifies whether to insert a document when no matching document is found, and multi specifies whether all matching documents should be updated.

  • Delete documents

    > db.products.remove(
            {name: {$eq: 'sample book'}}
        )
    

    Deletes all documents that match the query specified in the first argument.

References