First steps with MongoDB (II). Database creation, collections and CRUD operations on documents

    share on:
    First steps with MongoDB (II). Database creation, collections and CRUD operations on documents

    Creating a database with MongoDB is really simple. First, we need to run mongo by running the command mongo (placed at C:\mongodb\bin). Then, change the initial database to our database in this manner:

    use diet

    Where diet is the name of our database (if that database does not exist, mongo will create for us). The message “switched to db diet” is shown.

    Once created our database, we are going to create a new collection. A collection is similar to a table in a relational schema. In our database, foods will be the name of a collection (of documents). MongoDB allows us to insert directly documents (similar to records in a relational database schema) in such collection (previously created or not).

    To create our collection, we use db.foods.insert({…}) statement. Inserting a document corresponds to a storage of a JSON file with a specific structure. In our case, a new food document could have the following structure:

    { food:{ type: "product", name: "orange juice", nutritionalvalues: { calcium: "0", protein: "1", cholesterol: "15", sodium: "150", sugars: "1" } servingsize: "2", patient_id: "001" } }

    1. Insert documents

    To insert the previous document in our collection foods, we use the command:

    db.foods.insert({type: "product", name: "orange juice", nutritional_values: {calcium: "0", protein: "1", cholesterol: "15", sodium: "150", sugars: "1"}, serving_size: "2", patient_id: "001"})

    This operation returns a WriteResult object with the status of the operation and the number of inserted documents (WriteResult({ "nInserted" : 1 })).

    Also, we can define variables which contain one or more documents

    var myfoods= [{food_one}, {food_two}, {food_three}];

    Thus, the command to insert all foods is:


    2. Find documents

    We can verify the insertion of the documents by querying the collection:


    The command will return the documents of the collection, specifying the internal ID of each one. If we need to search a specific document, we can use statements such as this one (it retrieves from the foods collection all documents where the type field has the value meal):

    db.foods.find({type: "meal"})

    3. Update documents

    To update a specific document, we use the command update() in this manner:

    db.foods.update({name: "orange juice"}, {$set: {"nutritional_values.sugars": "2"}})

    In this case, we have updated a document with the embedded field sugars, changing its value from “1” to “2”. This is an example of basic operation of update command.

    4. Remove documents

    To remove all documents from a collection, we have to use this command:


    If you want to remove a subset of document that match a deletion criteria (e.g. remove all documents from foods where the type field equals product) use this command:


    If you want to remove a single document, use the previous command in this way:

    db.foods.remove({type:"product"}, 1)

    For more information about CRUD operations visit the Mongo DB oficial website.

    Jesus Fontecha

    PhD in Computer Science