Skip to content

Home

Development

IDEs

API Explorer

Releases

Release notes

Martini Runtime

Martini Desktop

Martini Online

Feature requests

Bug reports

MongoDB query types

There are six types of MongoDB queries – find, insert, replace, update, delete, and aggregate. This page will go through each type, and demonstrate how to create and execute them using the MongoDB query editor.

The screenshot below shows the location of the Query Type field:

MongoDB uery Type Field

Assumed MongoDB documents for example queries

For demonstration purposes, let's assume the queried collection contains the following documents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[
    {
        _id : ObjectId("5d199bb842da29077dd5c0c7"),
        first_name : "Rumyantsev",
        last_name : "Savelievich",
        age : 35,
        gender : "male"
    },
    {
        _id : ObjectId("5d1d5fb48921584cfb6de57a"),
        first_name : "Agliullin",
        last_name : "Petrovich",
        age : 42,
        gender : "male"
    },
    {
        _id : ObjectId("5d199be442da29077dd5c0c9"),
        first_name : "Lvova",
        last_name : "Gennadievna",
        age : 26,
        gender : "female"
    },
    {
        _id : ObjectId("5d199bf042da29077dd5c0ca"),
        first_name : "Silina",
        last_name : "Ruslanovna",
        age : 40,
        gender : "female"
    }
]

Find query

A find query lets you fetch documents from a collection. To create and execute a find query, follow the steps below:

  1. Choose "Find" in the Query Type drop-down.

  2. Specify a filter for your documents using the Filter text area.

    In MongoDB, documents are filtered using field-value pairs. This filter will let you get documents whose gender field is set to male:

    1
    2
    3
    {
        gender : "male"
    }
    
  3. Next, specify which fields of the matching documents should be displayed in the results by using the Projection text area. This projection prompts MongoDB to return documents with their first_name and last_name fields.

    1
    2
    3
    4
    {
        first_name : 1,
        last_name : 1
    }
    
  4. Click the run button in the editor to execute your query. Results will be displayed in the Results area.

    With this query, the expected result from the dataset provided earlier is:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    [
        {
          "_id" : ObjectId("5d199bb842da29077dd5c0c7"),
          "first_name" : "Rumyantsev",
          "last_name" : "Savelievich"
        },
        {
          "_id" : ObjectId("5d1d5fb48921584cfb6de57a"),
          "first_name" : "Agliullin",
          "last_name" : "Petrovich"
        }
    ]
    

Insert query

An insert query allows you to create a document. To create and execute an insert query, follow the steps below:

  1. Choose "Insert" in the Query Type drop-down.
  2. Define the document you want to add in the Document text area. For example:

    1
    2
    3
    4
    5
    6
    {
        first_name : "Zhelezkina",
        last_name : "Innokentievna",
        age : 22,
        gender : "female"
    }
    
  3. Click the run button. The ID of the created document will be displayed in the Results area.

    1
    2
    3
    {
      "insertedId" : ObjectId("5d1c1976bdbacb041c47f233")
    }
    

Replace one query

The replace one query allows you to replace an existing document with a new one.

  1. Choose "Replace" in the Query Type drop-down.
  2. Filter which document should be replaced in the Filter text area.

    1
    2
    3
    4
    5
    6
    {
        first_name : "Rumyantsev",
        last_name : "Savelievich",
        age : 35,
        gender : "male"
    }
    
  3. Define the document you want to replace it with in the Document text area. For example:

    1
    2
    3
    4
    5
    6
    {
        first_name : "Charkova",
        last_name : "Kirillovna",
        age : 33,
        gender : "female"
    }
    
  4. Check Upsert if you want to create a document using the provided replacement if no match is found using the filter.

  5. Click the run button to execute your query. MongoDB will return something similar to the response below if the replacement was successful:

    1
    2
    3
    4
    {
      "matchedCount" : NumberLong(1),
      "modifiedCount" : NumberLong(1)
    }
    

Update one, Update many query

There are two types of update queries: (1) "update one" and (2) "update many". The former allows you to update the first document matching the filter1, whilst the latter allows you to update all documents matching the filter. To create and execute an update query, follow the steps below:

  1. Choose "Update One", or "Update Many" in the Query Type drop-down.
  2. Filter which document(s) should be updated in the Filter text area.

    1
    2
    3
    4
    5
    6
    {
        first_name : "Agliullin",
        last_name : "Petrovich",
        age : 42,
        gender : "male"
    }
    
  3. Specify the fields you want to update and their new values using the Update text area. Update data should be wrapped in a $set: {}.

    1
    2
    3
    4
    5
    {
        $set: {
            age : 32
        }
    }
    
  4. Check Upsert if you want to create a document using the provided update if no match is found using the filter.

  5. Click the run button to execute your query. MongoDB would return something like below if the update is successful:

    1
    2
    3
    4
    {
      "matchedCount" : NumberLong(1),
      "modifiedCount" : NumberLong(1)
    }
    

Delete one, delete many query

There are two types of delete queries: (1) "delete one" and (2) "delete many". The former allows you to delete the first document matching the filter1, whilst the latter allows you to delete all documents matching the filter. To create and execute an delete query, follow the steps below:

  1. Filter which document(s) would be deleted through the Filter text area.

    1
    2
    3
    4
    5
    6
    {
      first_name : "Agliullin",
      last_name : "Petrovich",
      age : 42,
      gender : "male"
    }
    
  2. Click the run button to execute your query. MongoDB would return something like below if deletion is successful:

    1
    2
    3
    4
    {
      "deletedCount" : NumberLong(1),
      "acknowledged" : true
    }
    

Aggregation query

An aggregation query allows you to transform documents in order to return an aggregated result. To create and execute an aggregation query, follow the steps below:

  1. Specify aggregation pipelines you want to use through the Pipelines text area. Pipelines define the required transformation process.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    [
        {
            $project: {
                male: { $cond: [ { $eq: [ "$gender", "male" ] }, 1, 0 ] },
                female: { $cond: [ { $eq: [ "$gender", "female" ] }, 1, 0 ] }
            }
        },
        {
            $group: {
                _id: null,
                male: { $sum: "$male" },
                female: { $sum: "$female" },
                total: { $sum: 1 }
            }
        }
    ]
    

    What the above query does is it will use a $cond that maps the gender, that is $eq to male or female, to 0 or 1. And then $group the $sum of each field.

  2. Click the run button to execute your query. The expected result from the dataset provided earlier is:

    1
    2
    3
    4
    5
    6
    {
      "_id" : null,
      "male" : 2,
      "female" : 2,
      "total" : 4
    }
    

  1. A filter is used to identify which document(s) to update.