One place for hosting & domains

      MongoDB

      How To Create Queries in MongoDB


      The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      Documents stored in a MongoDB database can vary widely. Some might be relatively small and contain only a few entries, like items in a shopping list. Others might be highly complex, containing dozens of fields of different types, arrays holding multiple values, and even other documents nested within the larger structure.

      Regardless of how complex your documents are or how many you have, most often you won’t need to review the data in all of them at once. Instead, you’ll more likely want to only retrieve documents that satisfy one or more particular conditions. Similar to how you would find your holiday destination by selecting a range of filters on a booking website, such as distance from the seaside, pet-friendliness, a pool, and nearby parking, you can precisely query MongoDB to find exactly the documents you need. MongoDB provides a robust query mechanism for defining filtering criteria when retrieving documents.

      In this tutorial, you’ll learn how to query MongoDB collections using a different range of filters and conditions. You will also learn what cursors are and how to use them within the MongoDB shell.

      Prerequisites

      To follow this tutorial, you will need:

      Note: The linked tutorials on how to configure your server, install, and then secure MongoDB installation refer to Ubuntu 20.04. This tutorial concentrates on MongoDB itself, not the underlying operating system. It will generally work with any MongoDB installation regardless of the operating system as long as authentication has been enabled.

      Step 1 — Preparing the Sample Database

      To explain how to create queries in MongoDB — including how to filter documents with multiple fields, nested documents, and arrays — this guide uses an example database containing a collection of documents that describe the five highest mountains in the world.

      To create this sample collection, connect to the MongoDB shell as your administrative user. This tutorial follows the conventions of the prerequisite MongoDB security tutorial and assumes the name of this administrative user is AdminSammy and its authentication database is admin. Be sure to change these details in the following command to reflect your own setup, if different:

      • mongo -u AdminSammy -p --authenticationDatabase admin

      When prompted, enter the password you set when you created your administrative user. After providing the password, your prompt will change to a greater-than (>) sign:

      Note: On a fresh connection, the MongoDB shell will automatically connect to the test database by default. You can safely use this database to experiment with MongoDB and the MongoDB shell.

      Alternatively, you could also switch to another database to run all of the example commands given in this tutorial. To switch to another database, run the use command followed by the name of your database:

      To understand how MongoDB filters documents with multiple fields, nested documents and arrays, you’ll need sample data complex enough to allow exploring different types of queries. As mentioned previously, this guide uses a sample collection of the five highest mountains in the world.

      The documents in this collection will follow this format. This example document describes Mount Everest:

      Mount Everest document

      {
          "name": "Everest",
          "height": 8848,
          "location": ["Nepal", "China"],
          "ascents": {
              "first": {
                  "year": 1953,
              },
              "first_winter": {
                  "year": 1980,
              },
              "total": 5656,
          }
      }
      

      This document contains the following fields and values:

      • name: the peak’s name
      • height: the peak’s elevation, in meters
      • location: the countries in which the mountain is located. This field stores values as an array to allow for mountains located in more than one country
      • ascents: this field’s value is another document. When one document is stored within another document like this, it’s known as an embedded or nested document. Each ascents document describes successful ascents of the given mountain. Specifically, each ascents document contains a total field that lists the total number of successful ascents of each given peak. Additionally, each of these nested documents contain two fields whose values are also nested documents:
        • first: this field’s value is a nested document that contains one field, year, which describes the year of the first overall successful ascent
        • first_winter: this field’s value is a nested document that also contains a year field, the value of which represents the year of the first successful winter ascent of the given mountain

      The reason why the first ascents are represented as nested documents even though only the year is included now is to make it easier to expand the ascent details with more fields in the future, such as the summiters’ names or the expedition details.

      Run the following insertMany() method in the MongoDB shell to simultaneously create a collection named peaks and insert five sample documents into it. These documents describe the five tallest mountain peaks in the world:

      • db.peaks.insertMany([
      • {
      • "name": "Everest",
      • "height": 8848,
      • "location": ["Nepal", "China"],
      • "ascents": {
      • "first": {
      • "year": 1953
      • },
      • "first_winter": {
      • "year": 1980
      • },
      • "total": 5656
      • }
      • },
      • {
      • "name": "K2",
      • "height": 8611,
      • "location": ["Pakistan", "China"],
      • "ascents": {
      • "first": {
      • "year": 1954
      • },
      • "first_winter": {
      • "year": 1921
      • },
      • "total": 306
      • }
      • },
      • {
      • "name": "Kangchenjunga",
      • "height": 8586,
      • "location": ["Nepal", "India"],
      • "ascents": {
      • "first": {
      • "year": 1955
      • },
      • "first_winter": {
      • "year": 1986
      • },
      • "total": 283
      • }
      • },
      • {
      • "name": "Lhotse",
      • "height": 8516,
      • "location": ["Nepal", "China"],
      • "ascents": {
      • "first": {
      • "year": 1956
      • },
      • "first_winter": {
      • "year": 1988
      • },
      • "total": 461
      • }
      • },
      • {
      • "name": "Makalu",
      • "height": 8485,
      • "location": ["China", "Nepal"],
      • "ascents": {
      • "first": {
      • "year": 1955
      • },
      • "first_winter": {
      • "year": 2009
      • },
      • "total": 361
      • }
      • }
      • ])

      The output will contain a list of object identifiers assigned to the newly-inserted objects.

      Output

      { "acknowledged" : true, "insertedIds" : [ ObjectId("610c23828a94efbbf0cf6004"), ObjectId("610c23828a94efbbf0cf6005"), ObjectId("610c23828a94efbbf0cf6006"), ObjectId("610c23828a94efbbf0cf6007"), ObjectId("610c23828a94efbbf0cf6008") ] }

      You can verify that the documents were properly inserted by running the find() method with no arguments, which will retrieve all the documents you just added:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } . . .

      With that, you have successfully created the list of example documents of mountains that will serve as the test data for creating queries. Next, you’ll learn how to query with conditions referring to individual fields.

      Step 2 — Querying Individual Fields

      At the end of the previous step, you used MongoDB’s find() method to return every document from the peaks collection. A query like this won’t be very useful in practice, though, as it doesn’t filter any documents and always returns the same result set.

      You can filter query results in MongoDB by defining a specific condition that documents must adhere to in order to be included in a result set. If you have followed the How To Perform CRUD operations in MongoDB tutorial, you have already used the most basic filtering condition: the equality condition.

      As an example, run the following query which returns any documents whose name value is equal to Everest:

      • db.peaks.find(
      • { "name": "Everest" }
      • )

      The second line — { "name": "Everest" } — is the query filter document, a JSON object specifying the filters to apply when searching the collection in order to find documents that satisfy the condition. This example operation tells MongoDB to retrieve any documents in the peaks collection whose name value matches the string Everest:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } }

      MongoDB returned a single document, as there is only one Mt. Everest in the peaks collection.

      The equality condition specifies a single value that MongoDB will attempt to match against documents in the collection. MongoDB provides comparison query operators that allow you to specify other conditions that also refer to a single field, but filter documents in ways that are more complex than searching for exact matches.

      A comparison operator consists of the operator itself, a single key preceded by a dollar sign ($), and the value the query operator will use to filter documents.

      To illustrate, run the following query which searches for any documents whose name value does not equal Everest:

      • db.peaks.find(
      • { "name": { $ne: "Everest" } }
      • )

      This time, the query filter document includes { $ne: "Everest" }. $ne is the comparison operator in this example, and it stands for “not equal”. The peak name, Everest, appears again as the value for this operator. Because this query is searching for documents whose name value is not equal to Everest, it returns four documents:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6005"), "name" : "K2", "height" : 8611, "location" : [ "Pakistan", "China" ], "ascents" : { "first" : { "year" : 1954 }, "first_winter" : { "year" : 1921 }, "total" : 306 } } { "_id" : ObjectId("610c23828a94efbbf0cf6006"), "name" : "Kangchenjunga", "height" : 8586, "location" : [ "Nepal", "India" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 1986 }, "total" : 283 } } . . .

      The $in operator allows you to write queries that will return documents with values matching one of multiple values held in an array.

      The following example query includes the $in operator, and will return documents whose name value matches either Everest or K2:

      • db.peaks.find(
      • { "name": { $in: ["Everest", "K2"] } }
      • )

      Instead of a single value, the value passed to the $in operator is an array of two peak names in square braces. MongoDB returns two documents, just as expected:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "_id" : ObjectId("610c23828a94efbbf0cf6005"), "name" : "K2", "height" : 8611, "location" : [ "Pakistan", "China" ], "ascents" : { "first" : { "year" : 1954 }, "first_winter" : { "year" : 1921 }, "total" : 306 } }

      The examples so far have queried the name field with text values. You can also filter documents based on numerical values.

      The following example query searches for documents whose height value is greater than 8500:

      • db.peaks.find(
      • { "height": { $gt: 8500 } }
      • )

      This query includes the $gt operator, which stands for greater than. By passing it the value 8500, MongoDB will return documents whose height value is greater than 8500:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "_id" : ObjectId("610c23828a94efbbf0cf6005"), "name" : "K2", "height" : 8611, "location" : [ "Pakistan", "China" ], "ascents" : { "first" : { "year" : 1954 }, "first_winter" : { "year" : 1921 }, "total" : 306 } } { "_id" : ObjectId("610c23828a94efbbf0cf6006"), "name" : "Kangchenjunga", "height" : 8586, "location" : [ "Nepal", "India" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 1986 }, "total" : 283 } } { "_id" : ObjectId("610c23828a94efbbf0cf6007"), "name" : "Lhotse", "height" : 8516, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1956 }, "first_winter" : { "year" : 1988 }, "total" : 461 } }

      MongoDB offers a number of comparison query operators in addition to the ones outlined in this section. For a full list of these operators, see the official documentation on the subject.

      Now that you know how to use equality conditions and comparison operators on a single document field, you can move onto learning how to join multiple conditions together in a single query.

      Step 3 — Using Multiple Conditions

      Sometimes, filtering based on a single document field is not enough to precisely select documents of interest. In such cases, you might want to filter documents using multiple conditions at once.

      There are two ways to connect multiple conditions in MongoDB. The first is to use a logical AND conjunction to select documents in the collection matching all the conditions, or the logical OR to select documents matching at least one condition from the list.

      In MongoDB, the AND conjunction is implicit when using more than one field in the query filter document. Try selecting a mountain that matches the name Everest and the exact height of 8848 meters:

      • db.peaks.find(
      • { "name": "Everest", "height": 8848 }
      • )

      Notice that the syntax is similar to the equality condition example from the previous step, but this time two fields appear in the query filter document. MongoDB checks for equality on both fields and requires both to match the requested values in order for a document to be selected:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } }

      In this case, a single document is returned, but if you try changing the height to any other numerical value the result set will be empty since any returned documents must match both conditions. For instance, the following example will not return any output to the shell:

      • db.peaks.find(
      • { "name": "Everest", "height": 9000 }
      • )

      This implicit AND can be made explicit by including the $and logical query operator followed by a list of conditions that returned documents must satisfy. The following example is essentially the same query as the previous one, but includes the $and operator instead of an implicit AND conjunction:

      • db.peaks.find(
      • { $and: [{"name": "Everest"}, {"height": 8848}] }
      • )

      This time the JSON object containing the $and query operator is the query filter document itself. Here, the comparison operator takes two separate equality conditions that appear in the list, one for name matches and the latter for height matches.

      In order to select documents matching any of the chosen conditions rather than all of them, you can instead use the $or operator:

      • db.peaks.find(
      • { $or: [{"name": "Everest"}, {"name": "K2"}] }
      • )

      When using the $or operator, a document only needs to satisfy one of the two the equality filters:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "_id" : ObjectId("610c23828a94efbbf0cf6005"), "name" : "K2", "height" : 8611, "location" : [ "Pakistan", "China" ], "ascents" : { "first" : { "year" : 1954 }, "first_winter" : { "year" : 1921 }, "total" : 306 } }

      Although each of this example’s conditions are single-field equality conditions, both the $and and $or operators can contain any valid query filter documents. They can even include nested AND/OR condition lists.

      Joining multiple filters together using $and and $or operators as outlined in this step can be very helpful with retrieving fine-grained query results. However, the examples so far have all used query filter documents that filter based on individual values. The next step outlines how to query against values stored in an array field.

      Step 4 — Querying for Array Values

      Sometimes a single field may contain multiple values stored in an array. In our example with mountain peaks, location is such a field. Because mountains often span more than one country, like Kangchenjunga in Nepal and India, a single value may not always be enough for this field.

      In this step, you’ll learn how to construct query filters that match items in array fields.

      Let’s start by trying to select documents representing mountains that are in Nepal. For this example, though, it’s okay if the mountain has multiple locations listed, as long as one of them is Nepal:

      • db.peaks.find(
      • { "location": "Nepal" }
      • )

      This query uses an equality condition that tells MongoDB to return documents whose location value exactly matches the given string value, Nepal, similar to the previous examples that used the name field. MongoDB will select any documents in which the requested value appears in any place in the arrays:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "_id" : ObjectId("610c23828a94efbbf0cf6006"), "name" : "Kangchenjunga", "height" : 8586, "location" : [ "Nepal", "India" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 1986 }, "total" : 283 } } { "_id" : ObjectId("610c23828a94efbbf0cf6007"), "name" : "Lhotse", "height" : 8516, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1956 }, "first_winter" : { "year" : 1988 }, "total" : 461 } } { "_id" : ObjectId("610c23828a94efbbf0cf6008"), "name" : "Makalu", "height" : 8485, "location" : [ "China", "Nepal" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 2009 }, "total" : 361 } }

      For this query, MongoDB returned the four documents in which Nepal appears in the location field.

      However, what if you wanted to find mountains located in both China and Nepal? To do this, you could include an array in the filter document, rather than a single value:

      • db.peaks.find(
      • { "location": ["China", "Nepal"] }
      • )

      Even though there are four mountains in Nepal and China in the database, there is only one in which the countries are listed in the order given in this query, so this query returns a single document:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6008"), "name" : "Makalu", "height" : 8485, "location" : [ "China", "Nepal" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 2009 }, "total" : 361 } }

      Notice that the value of the location field for Makalu is identical to the query’s filter document. When you supply an array as the value for the equality condition like this, MongoDB will retrieve documents where the location field matches the query filter exactly, including the order of elements inside the array. To illustrate, run the query again but swap China with Nepal:

      • db.peaks.find(
      • { "location": ["Nepal", "China"] }
      • )

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "_id" : ObjectId("610c23828a94efbbf0cf6007"), "name" : "Lhotse", "height" : 8516, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1956 }, "first_winter" : { "year" : 1988 }, "total" : 461 } }

      Now, two other mountains are returned, but Makalu is not.

      Using the equality condition like this is not helpful in cases where you care only about elements in an array (regardless of their order) rather than an exact match. Fortunately, MongoDB allows you to retrieve documents containing more than one array element anywhere in an array using the $all query operator.

      To illustrate, run the following query:

      • db.peaks.find(
      • { "location": { $all: ["China", "Nepal"] } }
      • )

      The $all operator will ensure that documents will be checked whether their location array contains both China and Nepal inside in any order. MongoDB will return all three mountains in a single query:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "_id" : ObjectId("610c23828a94efbbf0cf6007"), "name" : "Lhotse", "height" : 8516, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1956 }, "first_winter" : { "year" : 1988 }, "total" : 461 } } { "_id" : ObjectId("610c23828a94efbbf0cf6008"), "name" : "Makalu", "height" : 8485, "location" : [ "China", "Nepal" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 2009 }, "total" : 361 } }

      This step outlined how to use arrays in query filter documents to retrieve documents with more than one value in a single field. If you want to query data held within a nested document, you’ll need to use the special syntax required for such an operation. Continue onto the next step to learn how to do this.

      Step 5 — Querying Fields in Nested Documents

      Recall that the example database documents include an ascent field that holds various details about each mountain’s first ascents an array. This way, the data about the first ascent, the winter ascent, and the total number of ascents is cleanly grouped inside a single nested document. This step explains how you can access fields within a nested document when building queries.

      Review the sample Everest document once more:

      The Everest document

      {
          "name": "Everest",
          "height": 8848,
          "location": ["Nepal", "China"],
          "ascents": {
              "first": {
                  "year": 1953,
              },
              "first_winter": {
                  "year": 1980,
              },
              "total": 5656,
          }
      }
      

      Accessing the name and height fields was straightforward, as a single value resides under these keys. But say you wanted to find the total number of ascents for a given peak. The ascents field contains more data than just the total number of ascents inside. There is a total field, but it’s not part of the main document, so there’s no way to access it directly.

      To solve this issue, MongoDB provides a dot notation to access fields in nested documents.

      To illustrate how MongoDB’s dot notation works, run the following query. This will return all the mountains in the collection that have been ascended more than 1000 times, using the $gt operator highlighted previously:

      • db.peaks.find(
      • { "ascents.total": { $gt: 1000 } }
      • )

      Mt. Everest is the only mountain in the collection with more than 1000 ascents, so only its document will be returned:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848, "location" : [ "Nepal", "China" ], "ascents" : { "first" : { "year" : 1953 }, "first_winter" : { "year" : 1980 }, "total" : 5656 } }

      While the { $gt: 1000 } query filter with $gt operator is familiar, notice how this query accesses the total field held within the document stored in the ascents field. In nested documents, the access path to any given field is constructed with dots indicating the action of going inside the nested object.

      So, ascents.total means that MongoDB should first open the nested document that the ascents field points to and then find the total field within it.

      The notation works with multiple nested documents as well:

      • db.peaks.find(
      • { "ascents.first_winter.year": { $gt: 2000 } }
      • )

      This query will return any documents describing mountains that were first ascended in winter only after the year 2000:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6008"), "name" : "Makalu", "height" : 8485, "location" : [ "China", "Nepal" ], "ascents" : { "first" : { "year" : 1955 }, "first_winter" : { "year" : 2009 }, "total" : 361 } }

      As before, the ascents.first_winter.year notation means MongoDB first finds the ascents field and finds the nested documents there. It then goes into another nested document, first_winter, and finally retrieves the year field from within it.

      The dot notation can be used to access any depth of nested documents in MongoDB.

      By now you will have a good understanding of how to access data from nested documents and how to filter query results. You can move on to learning how to limit the list of fields returned by your queries.

      Step 6 — Returning a Subset of Fields

      In all the examples so far, whenever you queried the peaks collection, MongoDB returned one or more full documents. Oftentimes, you’ll only need information from a handful of fields. As an example, you might only want to find the names of the mountains in the database.

      This isn’t just a matter of legibility, but also of performance. If only a small part of a document is needed, retrieving whole document objects would be an unnecessary performance burden on the database. This may not be a problem when working with small datasets like this tutorial’s examples, but it becomes an important consideration when working with many large, complex documents.

      As an example, say you’re only interested in mountain names stored in the peaks collection, but the ascent details or location are not important this time. You could limit the fields your query will return by following the query filter document with a projection.

      A projection document is a JSON object where keys correspond to the fields of the queried documents. Projections can be either constructed as inclusion projections or exclusion projections. When the projection document contains keys with 1 as their values, it describes the list of fields that will be included in the result. If, on the other hand, projection keys are set to 0, the projection document describes the list of fields that will be excluded from the result.

      Run the following query, which includes the by-now familiar find() method. This query’s find() method includes two arguments, instead of one. The first, {}, is the query filter document. Here it’s an empty JSON object, meaning it won’t apply any filtering. The second argument, { "name": 1 }, describes the projection and means that the query results will only include each document’s name field:

      • db.peaks.find(
      • {},
      • { "name": 1 }
      • )

      After running this example query, MongoDB returns the following results:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest" } { "_id" : ObjectId("610c23828a94efbbf0cf6005"), "name" : "K2" } { "_id" : ObjectId("610c23828a94efbbf0cf6006"), "name" : "Kangchenjunga" } { "_id" : ObjectId("610c23828a94efbbf0cf6007"), "name" : "Lhotse" } { "_id" : ObjectId("610c23828a94efbbf0cf6008"), "name" : "Makalu" }

      Notice that the returned documents are simplified, and contain only the name and _id fields. MongoDB always includes the _id key, even if it’s not explicitly requested.

      To illustrate how to specify what fields to exclude, run the following query. It will return data from each document, but will exclude the ascents and location fields:

      • db.peaks.find(
      • {},
      • { "ascents": 0, "location": 0 }
      • )

      MongoDB returns all five mountains once again, but this time only the name, height, and _id fields are present:

      Output

      { "_id" : ObjectId("610c23828a94efbbf0cf6004"), "name" : "Everest", "height" : 8848 } { "_id" : ObjectId("610c23828a94efbbf0cf6005"), "name" : "K2", "height" : 8611 } { "_id" : ObjectId("610c23828a94efbbf0cf6006"), "name" : "Kangchenjunga", "height" : 8586 } { "_id" : ObjectId("610c23828a94efbbf0cf6007"), "name" : "Lhotse", "height" : 8516 } { "_id" : ObjectId("610c23828a94efbbf0cf6008"), "name" : "Makalu", "height" : 8485 }

      Note: When specifying projections, you cannot mix inclusions and exclusions. You either have to specify the list of fields to include, or a list of fields to exclude.

      There is, however, one exception to this rule. MongoDB allows you to exclude the _id field from a result set even when the query has an inclusion projection applied. To suppress the _id field, you can append "_id": 0 to the projection document. The following example is similar to the previous example query, but will exclude every field, including _id, except for the name field:

      • db.peaks.find(
      • {},
      • { "_id": 0, "name": 1 }
      • )

      Output

      { "name" : "Everest" } { "name" : "K2" } { "name" : "Kangchenjunga" } { "name" : "Lhotse" } { "name" : "Makalu" }

      Projections can also be used to include or exclude fields in nested documents. Say, for example, that you want to know each mountain’s first winter ascent and the total number of ascents, both of which are nested within the ascents field. Additionally, you want to return each mountain’s name. To do this, you could run a query like this:

      • db.peaks.find(
      • {},
      • { "_id": 0, "name": 1, "ascents": { "first_winter": 1, "total": 1 } }
      • )

      Notice how the projection is specified for the ascents fields and how it follows the structure of the nested document, being a nested projection itself. By using "first_winter": 1, "total": 1 this query tells the database to include only these two fields from the nested document and no other.

      The returned documents will contain only the requested fields:

      Output

      { "name" : "Everest", "ascents" : { "first_winter" : { "year" : 1980 }, "total" : 5656 } } { "name" : "K2", "ascents" : { "first_winter" : { "year" : 1921 }, "total" : 306 } } { "name" : "Kangchenjunga", "ascents" : { "first_winter" : { "year" : 1988 }, "total" : 461 } } { "name" : "Makalu", "ascents" : { "first_winter" : { "year" : 2009 }, "total" : 361 } }

      Limiting the size of returned documents to only a subset of fields can be helpful with making result sets more readable and can even improve performance. The next step outlines how to limit the number of documents returned by a query, and also details how to sort the data returned by a query.

      Step 7 — Using Cursors to Sort and Limit Query Results

      When retrieving objects from a large collection, there may be times when you want to limit the number of results or perhaps sort them in a particular order. For example, a popular approach for shopping sites is to sort products by their price. MongoDB uses cursors which allow you to limit the number of documents returned in a query result set and also sort the results in ascending or descending order.

      Recall this example query from Step 1:

      You may recall that the result set returned by this query includes all the data from each document in the peaks collection. While it may seem like MongoDB returns all the objects from the peaks collection, this is not the case. What MongoDB actually returns is a cursor object.

      A cursor is a pointer to the result set of a query but it is not the result set itself. It’s an object that can be iterated, meaning that you can request the cursor to return the next document in line, and only then will the full document be retrieved from the database. Until that happens, the cursor only points to the next document on the list.

      With cursors, MongoDB can ensure that the actual document retrieval happens only when it’s needed. This can have significant performance implications when the documents in question are large or many of them are requested at once.

      To illustrate how cursors work, run the following operation which includes both the find() and count() methods:

      MongoDB will respond with 5:

      Output

      5

      Under the hood, the find() method finds and then returns a cursor, and then the count() method is called on that cursor. This lets MongoDB know that you’re interested in the object count and not the documents themselves. This means that documents won’t be a part of the results — all the database will return is the count. Using methods on the cursor object to further modify the query before retrieving documents from the cursor, you can ensure only the database operations that you ask for will be performed on the collection.

      Note: When executing queries, the MongoDB shell automatically iterates over the returned cursors 20 times so as to display the first 20 results on the screen. This is specific to the MongoDB shell. When working with MongoDB programmatically, it won’t immediately retrieve any results from a cursor.

      Another MongoDB method that uses cursors to alter a result set is the limit() method. As its name implies, you can use limit() to limit the number of results a query will return.

      Run the following query which will retrieve only three mountain peaks from the collection:

      • db.peaks.find(
      • {},
      • { "_id": 0, "name": 1, "height": 1 }
      • ).limit(3)

      MongoDB shell will respond with three objects rather than five, even though the query isn’t filtering any data:

      Output

      { "name" : "Everest", "height" : 8848 } { "name" : "K2", "height" : 8611 } { "name" : "Kangchenjunga", "height" : 8586 }

      The limit(3) method applied on the cursor tells the cursor to stop returning further documents after reaching the first 3. Using the limit() cursor method like this with large collections will help to ensure that you only retrieve the results you need and no more.

      By default, MongoDB will return objects in the order of their insertion, but you might want to alter that behavior. Say you’re interested in finding the three lowest mountain peaks held in the database. You could run the following query:

      • db.peaks.find(
      • {},
      • { "_id": 0, "name": 1, "height": 1 }
      • ).limit(3).sort({ "height": 1 })

      The added sort({ "height": 1 }) causes the result set to differ from the previous example:

      Output

      { "name" : "Makalu", "height" : 8485 } { "name" : "Lhotse", "height" : 8516 } { "name" : "Kangchenjunga", "height" : 8586 }

      Again, only three mountain peaks are returned. However, this time they have been sorted ascending from the one with the lowest height value.

      The sort() method on the cursor accepts a JSON object — height — as an argument, similar to the projection document. It also accepts the list of keys that will be used to sort against. The accepted value is either 1 for ascending or -1 for descending sort order for each key.

      Conclusion

      By reading this article, you familiarized yourself with the way MongoDB uses to filter query results. You filtered collection documents against individual fields, multiple conditions, and complex structures such as arrays and nested documents. You have also learned to select only a subset of fields and sort the results using cursor methods. These techniques can be used to retrieve only documents of interest from otherwise large collections.

      The tutorial described only a handful of query operators put forward by MongoDB to allow precise document querying. You can study the official official MongoDB documentation to learn more about different query operators.



      Source link

      How To Perform CRUD operations in MongoDB


      The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      MongoDB is a persistent document-oriented database used to store and process data in the form of documents. As with other database management systems, MongoDB allows you to manage and interact with data through four fundamental types of data operations:

      • Create operations, which involve writing data to the database
      • Read operations, which query a database to retrieve data from it
      • Update operations, which change data that already exists in a database
      • Delete operations, which permanently remove data from a database

      These four operations are jointly referred to as CRUD operations.

      This tutorial outlines how to create new MongoDB documents and later retrieve them to read their data. It also explains how to update the data within documents, as well as how to delete documents when they are no longer needed.

      Prerequisites

      To follow this tutorial, you will need:

      Note: The linked tutorials on how to configure your server, install, and then secure MongoDB installation refer to Ubuntu 20.04. This tutorial concentrates on MongoDB itself, not the underlying operating system. It will generally work with any MongoDB installation regardless of the operating system as long as authentication has been enabled.

      Step 1 — Connecting to the MongoDB Server

      This guide involves using the MongoDB shell to interact with MongoDB. In order to follow along and practice CRUD operations in MongoDB, you must first connect to a MongoDB database by opening up the MongoDB shell.

      If your MongoDB instance is running on a remote server, SSH into that server from your local machine:

      Then connect to your MongoDB installation by opening up the MongoDB shell. Be sure to connect as a MongoDB user with privileges to write and read data. If you followed the prerequisite MongoDB security tutorial, you can connect as the administrative user you created in Step 1 of that guide:

      • mongo -u AdminSammy -p --authenticationDatabase admin

      After providing the user’s password, your terminal prompt will change to a greater-than sign (>). This means the shell is now ready to accept commands for the MongoDB server it’s connected to.

      Note: On a fresh connection, the MongoDB shell will automatically connect to the test database by default. You can safely use this database to experiment with MongoDB and the MongoDB shell.

      Alternatively, you could also switch to another database to run all of the example commands given in this tutorial. To switch to another database, run the use command followed by the name of your database:

      Now that you have connected to the MongoDB server using a MongoDB shell, you can move on to creating new documents.

      Step 2 — Creating Documents

      In order to have data that you can practice reading, updating, and deleting in the later steps of this guide, this step focuses on how to create data documents in MongoDB.

      Imagine that you’re using MongoDB to build and manage a directory of famous historical monuments from around the world. This directory will store information like each monument’s name, country, city, and geographical location.

      The documents in this directory will follow a format similar to this example, which represents The Pyramids of Giza:

      The Pyramids of Giza

      {
          "name": "The Pyramids of Giza",
          "city": "Giza",
          "country": "Egypt",
          "gps": {
              "lat": 29.976480,
              "lng": 31.131302
          }
      }
      

      This document, like all MongoDB documents, is written in BSON. BSON is a binary form of JSON, a human-readable data format. All data in BSON or JSON documents are represented as field-and-value pairs that take the form of field: value.

      This document consists of four fields. First is the name of the monument, followed by the city and the country. All three of these fields contain strings. The last field, called gps, is a nested document which details the monument’s GPS location. This location is made up of a pair of latitude and longitude coordinates, represented by the lat and lng fields respectively, each of which hold floating point values.

      Note: You can learn more about how MongoDB documents are structured in our conceptual article An Introduction to Document-Oriented Databases.

      Insert this document into a new collection called monuments using the insertOne method. As its name implies, insertOne is used to create individual documents, as opposed to creating multiple documents at once.

      In the MongoDB shell, run the following operation:

      • db.monuments.insertOne(
      • {
      • "name": "The Pyramids of Giza",
      • "city": "Giza",
      • "country": "Egypt",
      • "gps": {
      • "lat": 29.976480,
      • "lng": 31.131302
      • }
      • }
      • )

      Notice that you haven’t explicitly created the monuments collection before executing this insertOne method. MongoDB allows you to run commands on non-existent collections freely, and the missing collections only get created when the first object is inserted. By executing this example insertOne() method, not only will it insert the document into the collection but it will also create the collection automatically.

      MongoDB will execute the insertOne method and insert the requested document representing the Pyramids of Giza. The operation’s output will inform you that it executed successfully, and also provides the ObjectId which it generated automatically for the new document:

      Output

      { "acknowledged" : true, "insertedId" : ObjectId("6105752352e6d1ebb7072647") }

      In MongoDB, each document within a collection must have a unique _id field which acts as a primary key. You can include the _id field and provide it with a value of your own choosing, as long as you ensure each document’s _id field will be unique. However, if a new document omits the _id field, MongoDB will automatically generate an object identifier (in the form of an ObjectId object) as the value for the _id field.

      You can verify that the document was inserted by checking the object count in the monuments collection:

      Since you’ve only inserted one document into this collection, the count method will return 1:

      Output

      1

      Inserting documents one by one like this would quickly become tedious if you wanted to create multiple documents. MongoDB provides the insertMany method which you can use to insert multiple documents in a single operation.

      Run the following example command, which uses the insertMany method to insert six additional famous monuments into the monuments collection:

      • db.monuments.insertMany([
      • {"name": "The Valley of the Kings", "city": "Luxor", "country": "Egypt", "gps": { "lat": 25.746424, "lng": 32.605309 }},
      • {"name": "Arc de Triomphe", "city": "Paris", "country": "France", "gps": { "lat": 48.873756, "lng": 2.294946 }},
      • {"name": "The Eiffel Tower", "city": "Paris", "country": "France", "gps": { "lat": 48.858093, "lng": 2.294694 }},
      • {"name": "Acropolis", "city": "Athens", "country": "Greece", "gps": { "lat": 37.970833, "lng": 23.726110 }},
      • {"name": "The Great Wall of China", "city": "Huairou", "country": "China", "gps": { "lat": 40.431908, "lng": 116.570374 }},
      • {"name": "The Statue of Liberty", "city": "New York", "country": "USA", "gps": { "lat": 40.689247, "lng": -74.044502 }}
      • ])

      Notice the square brackets ([ and ]) surrounding the six documents. These brackets signify an array of documents. Within square brackets, multiple objects can appear one after another, delimited by commas. In cases where the MongoDB method requires more than one object, you can provide a list of objects in the form of an array like this one.

      MongoDB will respond with several object identifiers, one for each of the newly inserted objects:

      Output

      { "acknowledged" : true, "insertedIds" : [ ObjectId("6105770952e6d1ebb7072648"), ObjectId("6105770952e6d1ebb7072649"), ObjectId("6105770952e6d1ebb707264a"), ObjectId("6105770952e6d1ebb707264b"), ObjectId("6105770952e6d1ebb707264c"), ObjectId("6105770952e6d1ebb707264d") ] }

      You can verify that the documents were inserted by checking the object count in the monuments collection:

      After adding these six new documents, the expected output of this command is 7:

      Output

      7

      With that, you have used two separate insertion methods to create a number of documents representing several famous monuments. Next, you will read the data you just inserted with MongoDB’s find() method.

      Step 3 — Reading Documents

      Now that your collection has some documents stored within it, you can query your database to retrieve these documents and read their data. This step first outlines how to query all of the documents in a given collection, and then describes how to use filters to narrow down the list of retrieved documents.

      After completing the previous step, you now have seven documents describing famous monuments inserted into the monuments collection. You can retrieve all seven documents with a single operation using the find() method:

      This method, when used without any arguments, doesn’t apply any filtering and asks MongoDB to return all objects available in the specified collection, monuments. MongoDB will return the following output:

      Output

      { "_id" : ObjectId("6105752352e6d1ebb7072647"), "name" : "The Pyramids of Giza", "city" : "Giza", "country" : "Egypt", "gps" : { "lat" : 29.97648, "lng" : 31.131302 } } { "_id" : ObjectId("6105770952e6d1ebb7072648"), "name" : "The Valley of the Kings", "city" : "Luxor", "country" : "Egypt", "gps" : { "lat" : 25.746424, "lng" : 32.605309 } } { "_id" : ObjectId("6105770952e6d1ebb7072649"), "name" : "Arc de Triomphe", "city" : "Paris", "country" : "France", "gps" : { "lat" : 48.873756, "lng" : 2.294946 } } { "_id" : ObjectId("6105770952e6d1ebb707264a"), "name" : "The Eiffel Tower", "city" : "Paris", "country" : "France", "gps" : { "lat" : 48.858093, "lng" : 2.294694 } } { "_id" : ObjectId("6105770952e6d1ebb707264b"), "name" : "Acropolis", "city" : "Athens", "country" : "Greece", "gps" : { "lat" : 37.970833, "lng" : 23.72611 } } { "_id" : ObjectId("6105770952e6d1ebb707264c"), "name" : "The Great Wall of China", "city" : "Huairou", "country" : "China", "gps" : { "lat" : 40.431908, "lng" : 116.570374 } } { "_id" : ObjectId("6105770952e6d1ebb707264d"), "name" : "The Statue of Liberty", "city" : "New York", "country" : "USA", "gps" : { "lat" : 40.689247, "lng" : -74.044502 } }

      The MongoDB shell prints out all seven documents one by one and in full. Notice that each of these objects have an _id property which you didn’t define. As mentioned previously, the _id fields serve as their respective documents’ primary key, and were created automatically when you ran the insertMany method in the previous step.

      The default output from the MongoDB shell is compact, with each document’s fields and values printed in a single line. This can become difficult to read with objects containing multiple fields or nested documents, in particular.

      To make the find() method’s output more readable, you can use its pretty printing feature, like this:

      • db.monuments.find().pretty()

      This time, the MongoDB shell will print the documents on multiple lines, each with indentation:

      Output

      { "_id" : ObjectId("6105752352e6d1ebb7072647"), "name" : "The Pyramids of Giza", "city" : "Giza", "country" : "Egypt", "gps" : { "lat" : 29.97648, "lng" : 31.131302 } } { "_id" : ObjectId("6105770952e6d1ebb7072648"), "name" : "The Valley of the Kings", "city" : "Luxor", "country" : "Egypt", "gps" : { "lat" : 25.746424, "lng" : 32.605309 } } . . .

      Notice that in the two previous examples, the find() method was executed without any arguments. In both cases, it returned every object from the collection. You can apply filters to a query to narrow down the results.

      Recall from the previous examples that MongoDB automatically assigned The Valley of the Kings an object identifier with the value of ObjectId("6105770952e6d1ebb7072648"). The object identifier is not just the hexadecimal string inside the ObjectId(""), but the whole ObjectId object — a special datatype used in MongoDB to store object identifiers.

      The following find() method returns a single object by accepting a query filter document as an argument. Query filter documents follow the same structure as the documents you insert into a collection, consisting of fields and values, but they’re instead used to filter query results.

      The query filter document used in this example includes the _id field, with The Valley of the Kings’ object identifier as the value. To run this query on your own database, be sure to replace the highlighted object identifier with that of one of the documents stored in your own monuments collection:

      • db.monuments.find({"_id": ObjectId("6105770952e6d1ebb7072648")}).pretty()

      The query filter document in this example uses the equality condition, meaning the query will return any documents that have a field and value pair matching the one specified in the document. Essentially, this example tells the find() method to only return the documents whose _id value is equal to ObjectId("6105770952e6d1ebb7072648").

      After executing this method, MongoDB will return a single object matching the requested object identifier:

      Output

      { "_id" : ObjectId("6105770952e6d1ebb7072648"), "name" : "The Valley of the Kings", "city" : "Luxor", "country" : "Egypt", "gps" : { "lat" : 25.746424, "lng" : 32.605309 } }

      You can use quality condition on any other field from the document as well. To illustrate, try searching for monuments in France:

      • db.monuments.find({"country": "France"}).pretty()

      This method will return two monuments:

      Output

      { "_id" : ObjectId("6105770952e6d1ebb7072649"), "name" : "Arc de Triomphe", "city" : "Paris", "country" : "France", "gps" : { "lat" : 48.873756, "lng" : 2.294946 } } { "_id" : ObjectId("6105770952e6d1ebb707264a"), "name" : "The Eiffel Tower", "city" : "Paris", "country" : "France", "gps" : { "lat" : 48.858093, "lng" : 2.294694 } }

      Query filter documents are quite powerful and flexible, and they allow you to apply complex filters to collection documents.

      Step 4 — Updating Documents

      It’s common for documents within a document-oriented database like MongoDB to change over time. Sometimes, their structures must evolve along with the changing requirements of an application, or the data itself might change. This step focuses on how to update existing documents by changing field values in individual documents as well as and adding a new field to every document in a collection.

      Similar to the insertOne() and insertMany() methods, MongoDB provides methods that allow you to update either a single document or multiple documents at once. An important difference with these update methods is that, when creating new documents, you only need to pass the document data as method arguments. To update an existing document in the collection, you must also pass an argument that specifies which document you want to update.

      To allow users to do this, MongoDB uses the same query filter document mechanism in update methods as the one you used in the previous step to find and retrieve documents. Any query filter document that can be used to retrieve documents can also be used to specify documents to update.

      Try changing the name of Arc de Triomphe to the full name of Arc de Triomphe de l'Étoile. To do so, use the updateOne() method which updates a single document:

      • db.monuments.updateOne(
      • { "name": "Arc de Triomphe" },
      • {
      • $set: { "name": "Arc de Triomphe de l'Étoile" }
      • }
      • )

      The first argument of the updateOne method is the query filter document with a single equality condition, as covered in the previous step. In this example, { "name": "Arc de Triomphe" } finds documents with name key holding the value of Arc de Triomphe. Any valid query filter document can be used here.

      The second argument is the update document, specifying what changes should be applied during the update. The update document consists of update operators as keys, and parameters for each of the operator as values. In this example, the update operator used is $set. It is responsible for setting document fields to new values and requires a JSON object with new field values. Here, set: { "name": "Arc de Triomphe de l'Étoile" } tells MongoDB to set the value of field name to Arc de Triomphe de l'Étoile.

      The method will return a result telling you that one object was found by the query filter document, and also one object was successfully updated.

      Output

      { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

      Note: If the document query filter is not precise enough to select a single document, updateOne() will update only the first document returned from multiple results.

      To check whether the update worked, try retrieving all the monuments related to France:

      • db.monuments.find({"country": "France"}).pretty()

      This time, the method returns Arc de Triomphe but with its full name, which was changed by the update operation:

      Output

      { "_id" : ObjectId("6105770952e6d1ebb7072649"), "name" : "Arc de Triomphe de l'Étoile", "city" : "Paris", "country" : "France", "gps" : { "lat" : 48.873756, "lng" : 2.294946 } } . . .

      To modify more than one document, you can instead use the updateMany() method.

      As an example, say you notice there is no information about who created the entry and you’d like to credit the author who added each monument to the database. To do this, you’ll add a new editor field to each document in the monuments collection.

      The following example includes an empty query filter document. By including an empty query document, this operation will match every document in the collection and the updateMany() method will affect each of them . The update document adds a new editor field to each document, and assigns it a value of Sammy:

      • db.monuments.updateMany(
      • { },
      • {
      • $set: { "editor": "Sammy" }
      • }
      • )

      This method will return the following output:

      Output

      { "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }

      This output informs you that seven documents were matched and seven were also modified.

      Confirm that the changes were applied:

      • db.monuments.find().pretty()

      Output

      { "_id" : ObjectId("6105752352e6d1ebb7072647"), "name" : "The Pyramids of Giza", "city" : "Giza", "country" : "Egypt", "gps" : { "lat" : 29.97648, "lng" : 31.131302 }, "editor" : "Sammy" } { "_id" : ObjectId("6105770952e6d1ebb7072648"), "name" : "The Valley of the Kings", "city" : "Luxor", "country" : "Egypt", "gps" : { "lat" : 25.746424, "lng" : 32.605309 }, "editor" : "Sammy" } . . .

      All the returned documents now have a new field called editor set to Sammy. By providing a non-existing field name to the $set update operator, the update operation will create missing fields in all matched documents and properly set the new value.

      Although you’ll likely use $set most often, many other update operators are available in MongoDB, allowing you to make complex alterations to your documents’ data and structure. You can learn more about these update operators in MongoDB’s official documentation on the subject.

      Step 5 — Deleting Documents

      There are times when data in the database becomes obsolete and needs to be deleted. As with Mongo’s update and insertion operations, there is a deleteOne() method, which removes only the first document matched by the query filter document, and deleteMany(), which deletes multiple objects at once.

      To practice using these methods, begin by trying to remove the Arc de Triomphe de l'Étoile monument you modified previously:

      • db.monuments.deleteOne(
      • { "name": "Arc de Triomphe de l'Étoile" }
      • )

      Notice that this method includes a query filter document like the previous update and retrieval examples. As before, you can use any valid query to specify what documents will be deleted.

      MongoDB will return the following result:

      Output

      { "acknowledged" : true, "deletedCount" : 1 }

      Here, the result tells you how many documents were deleted in the process.

      Check whether the document has indeed been removed from the collection by querying for monuments in France:

      • db.monuments.find({"country": "France"}).pretty()

      This time the method returns only single monument, The Eiffel Tower, since you removed the Arc de Triomphe de l'Étoile:

      Output

      { "_id" : ObjectId("6105770952e6d1ebb707264a"), "name" : "The Eiffel Tower", "city" : "Paris", "country" : "France", "gps" : { "lat" : 48.858093, "lng" : 2.294694 }, "editor" : "Sammy" }

      To illustrate removing multiple documents at once, remove all the monument documents for which Sammy was the editor. This will empty the collection, as you’ve previously designated Sammy as the editor for every monument:

      • db.monuments.deleteMany(
      • { "editor": "Sammy" }
      • )

      This time, MongoDB lets you know that this method removed six documents:

      Output

      { "acknowledged" : true, "deletedCount" : 6 }

      You can verify that the monuments collection is now empty by counting the number of documents within it:

      Output

      0

      Since you’ve just removed all documents from the collection, this command returns the expected output of 0.

      Conclusion

      By reading this article, you became familiar with the concept of CRUD operations — Create, Read, Update and Delete — the four essential components of data management. You can now insert new documents into a MongoDB database, modify existing ones, retrieve documents already present in a collection, and also delete documents as needed.

      Be aware, though, that this tutorial covered only one fundamental way of query filtering. MongoDB offers a robust query system allowing to precisely select documents of interest against complex criteria. To learn more about creating more complex queries, we encourage you to check out the official MongoDB documentation on the subject.



      Source link

      How To Perform CRUD Operations in MongoDB Using PyMongo on Ubuntu 20.04


      The author selected the Free and Open Source Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      MongoDB is a general-purpose, document-oriented, NoSQL database program that uses JSON-like documents to store data. Unlike tabular relations used in relational databases, JSON-like documents allow for flexible and dynamic schemas while maintaining simplicity. In general, NoSQL databases have the ability to scale horizontally, making them suitable for big data and real-time applications.

      A database driver or connector is a program that connects an application to a database program. To perform CRUD operations in MongoDB using Python, a driver is required to establish the communication channel. PyMongo is the recommended driver for working with MongoDB from Python.

      In this guide, you will write a Python script that creates, retrieves, updates, and deletes data in a locally installed MongoDB server on Ubuntu 20.04. In the end, you will acquire relevant skills to understand the underlying concepts in how data moves across MongoDB and a Python application.

      Prerequisites

      Before you move forward with this guide, you will need the following:

      Step 1 — Setting Up PyMongo

      In this step, you will install PyMongo, the recommended driver for MongoDB from Python. As a collection of tools for working with MongoDB, PyMongo facilitates database requests using syntax and an interface native to Python.

      To enable PyMongo, open your Ubuntu terminal and install from the Python Package Index. It is recommended to install PyMongo within a virtual environment in order to isolate your Python project. Refer to this guide if you missed how to set up a virtual environment in the prerequisites.

      pip3 refers to the Python3 version of the popular pip package installer for Python. Note that within the Python 3 virtual environment you can use the command pip instead of pip3.

      Now, open the Python interpreter with the command below. The interpreter is a virtual machine that operates like a Unix shell, where you can execute Python code interactively.

      You are in the interpreter when you get an output similar to what’s below:

      Output

      Python 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

      With a successful output, import pymongo in the Python interpreter:

      Using the import statement, you can access the pymongo module and its code in your terminal. The import statement will run without raising exceptions.

      On the next line, import getpass.

      • from getpass import getpass

      getpass is a module for managing password inputs. The module prompts you for a password without showing an input, and adds a security mechanism to prevent displaying passwords as plaintext.

      Here, make a connection with MongoClient to enable a MongoDB instance of your database. Declare a variable client to hold the MongoClient instance with host, username, password, and authMechanism as arguments:

      • client = pymongo.MongoClient('localhost', username="username", password=getpass('Password: '), authMechanism='SCRAM-SHA-256')

      To connect to MongoDB with authorization enabled, MongoClient requires four arguments:

      • host - the hostname of the server on which MongoDB is installed. Since Mongo is local in this context, use localhost.
      • username and password - authorization credentials created after enabling authentication in MongoDB.
      • authMechanism - SCRAM-SHA-256 is the default authentication mechanism supported by a cluster configured for authentication with MongoDB 4.0 or later.

      Once you’ve established the client connection, you can now interact with your MongoDB instance.

      Step 2 — Testing Databases and Collections

      In this step, you will get familiar with NoSQL concepts such as collections and documents as applied to MongoDB.

      MongoDB supports managing multiple independent databases within a MongoClient instance. You can access or create a database using attribute style on a MongoClient instance. Declare a variable db and assign the new database as an attribute of client:

      In this context, the workplace database keeps track of employee records you will add such as the employee’s name and role.

      Next, create a collection. Like tables in relational databases, collections store a group of documents in MongoDB. In your Python interpreter, create an employees collection as an attribute of db and assign it to a variable of the same name:

      Create the employees collection as an attribute of db and assign it to a variable of the same name.

      Note: In MongoDB, databases, and collections are created lazily. This means that none of the above codes are actually executed until the first document is created.

      Now that you’ve reviewed collections, let’s look at how MongoDB represents documents, the basic structure for representing data.

      Step 3 — Performing CRUD Operations

      In this step, you will perform CRUD operations to manipulate data in MongoDB. Create, retrieve, update, and delete (CRUD) are the four basic operations in computer programming that one can perform to create persistent storage.

      To represent data in Python as JSON-like documents, dictionaries are used. Create a sample employee record with name and role attributes:

      • employee = {
      • "name": "Sammy",
      • "role": "Developer"
      • }

      As you can see, Python dictionaries are very similar in syntax to JSON documents. PyMongo converts Python dictionaries to JSON documents for scalable data storage.

      At this point, insert the employee record into the employees collection:

      • employees.insert_one(employee)

      Calling the insert_one() method on the employees collection, provide the employee record created earlier to be inserted. A successful insertion should return a successful output like below:

      Output

      <pymongo.results.InsertOneResult object at 0x7f8c5e3ed1c0>

      Now, verify you’ve successfully inserted the employee record and the collection. Make a query to find the employee you just created:

      • employees.find_one({"name": "Sammy"})

      Calling thefind_one() method on the employees collection with a name query returns a single matching document. This method is useful when you have only one document, or when you are interested in the first match.

      The output should look similar to this:

      Output

      {'_id': ObjectId('606ae5b2358ddf640da46894'), 'name': 'Sammy', 'role': 'Developer'}

      Note: When a document is inserted, a unique key _id is automatically added to the document if it does not already contain an _id key.

      If the need arises to modify existing documents, use the update_one() method. The update_one() method requires two arguments, query and update:

      • query - {"name": "Sammy"} - PyMongo will use this query parameter to find documents with elements that match.
      • update - { "$set": {"role": "Technical Writer"} } - The update parameter implements the $set operator, which replaces the value of a field with the specified value.

      Call the update_one() method on the employees collection:

      • employees.update_one({"name": "Sammy"}, { "$set": {"role": "Technical Writer"} })

      A successful update will return an output similar to this:

      Output

      <pymongo.results.UpdateResult object at 0x7f8c5e3eb940>

      To delete a single document, employ the delete_one() method. delete_one() requires a query parameter which specifies the document to delete. Execute the delete_one() method as an attribute of the employees collection with the name Sammy as a query parameter.

      • employees.delete_one({"name": "Sammy"})

      This will delete the only entry you have in your employees collection.

      Output

      <pymongo.results.DeleteResult object at 0x7f8c5e3c8280>

      Using the find_one() method again, it is apparent that you’ve successfully deleted Sammy’s employee record as nothing prints to the console.

      • employees.find_one({"name": "Sammy"})

      insert_one(), find_one(), update_one(), and delete_one() are great ways of getting started with performing CRUD operations in MongoDB with PyMongo.

      Conclusion

      In this guide, you have explored how to set up and configure PyMongo, the database driver, to connect Python code to MongoDB, as well as creating, retrieving, updating, and deleting documents. Although this guide focuses on introductory concepts, PyMongo offers more powerful and flexible ways of working with MongoDB. For instance, you can make bulk inserts, query for more than one document, add indexes to queries, and many more.

      To learn more about MongoDB management, see How To Back Up, Restore, and Migrate a MongoDB Database on Ubuntu 20.04 and How To Import and Export a MongoDB Database on Ubuntu 20.04.



      Source link