Querying data in MongoDB using MongoDB Compass and Docker

Marcos
4 min readMar 6, 2023

--

MongoDB main characteristics

  • MongoDB is a document-style database: the document-style database uses the equivalent of an object as its smallest logical unit. And a collection can contain multiple documents. Because there is no fixed schema, each document can have a different number and type of field, which adds to the flexibility.
  • Open source;
  • Runs in Linux, Mac, and Windows;
  • Versions Community, Enterprise, and Atlas;
  • Uses document format, which is stored inside collections. These documents have a structure very similar to JSON, with key and value.
  • The schema is not fixed. It means that we can have, within the same collection, several documents that have different fields. With this structure, MongoDB manages to facilitate the restructuring of a Database.
  • Provides replicas. With these replicas, it guarantees greater data availability, since, in addition to the main server, we can have two more copies of this server on other machines.

Data Types

MongoDB stores data records as BSON documents, which is a binary representation of JSON documents.

The value of a field in a document can be any of the BSON data types, including other documents, arrays, and document arrays.

  • NULL: stores null values
  • Boolean: can store true or false values
  • Number: signed number that can have an exponential E notation
  • Integer: can store integer data type in two forms, 32-bit signed integer and 64-bit signed integer
  • String: a sequence of one or more Unicode characters
  • Object: an unordered array of key/value items, also known as nested documents;
  • Array: stores an ordered list of any type, created using square brackets and with each element separated by commas
  • ObjectId: unique identifier of a MongoDB record
  • Date(): returns the current date in string format; It is
  • New Date() and ISODate(): return a date object

Reference: https://www.mongodb.com/docs/manual/reference/ bson-types/

Now, let’s run MongoDB using Docker

Create a docker-compose.yml file:

version: '3.1'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example

Run with this command:

docker-compose up

Using MongoDB Compass

Use this string connection: mongodb://root:example@localhost:27017/ to connect.

After, create a database and a collection:

Importing data

Here https://www.kaggle.com/datasets we can get a database to import and use in our study environment.

I will choose this database https://www.kaggle.com/datasets/bharathposa/rice-production-by-country-from-1961-to-2021?resource=download

Download it.

Now, import the file:

It’s done:

The documents are available:

Querying data with operators

My first query was this:

{"Value": "407000"}

Now, let’s use some operators.

$in

I wanna know how many records we have between the years 1999 and 2000.

{"Year": {$in: [ "1999",  "2000"]}}

$or

And I also see records from Brazil or Armenia:

{$or:[{"Area": "Brazil"}, {"Area": "Armenia"}]}

$lte

I wanna know how many records we have above 407000 tonnes. For this, I used the $lte operator

{"Value": {$lte: "407000"}}

$lt

I could use $lt, but the difference here is that by using $lte I can include the value in the results whereas with $lt the value will not be included:

{"Value": {$lt: "407000"}}

$gte

Finally, I wanna see records with 400.000 tonnes or more:

{"Value": {$gte: "400000"}}

That’s it for today.

--

--