Sitemap
Dalvolinda Silva

Dalvolinda Silva

Mainframe Analyst and Developer / Information Scientist

MongoDB: making queries

4 min readMar 3, 2024

This is a completed online course called MongoDB: making queries with an estimated workload of 10 hours, carrying out 60 of 60 activities, from 02/27/2024 to 03/01/2024. Topics include the following:

  • Preparing the database;
  • Find Method;
  • Aggregation stages;
  • Aggregation operators;
  • Other methods.

Preparing the database: after the Showbank2 database creation at NoSQLBooster platform, the command prompt was used in order to make the collections import through the mongoimport command. There are three collections: Accounts (Contas), Clients (Cientes) and Address (Endereco).

Showbank2 database creation
Press enter or click to view image in full size
The collections path informed at the command prompt
Press enter or click to view image in full size
Import of Accounts collection
Press enter or click to view image in full size
Import of Address collection
Press enter or click to view image in full size
Import of Clients collection
The imported collections

In order to follow the standard name in plural, the Address (Endereco) collection was renamed as Addresses (Enderecos) collection.

Press enter or click to view image in full size
Renamed Addresses collection
Final Showbank2 structure

Find Method: works with many different operators, such as: comparison ($eq, $gt, $gte, $in, $lt, $lte, $ne, $nin), logical ($and, $not, $nor, $or) and array ($all, $elemMatch, $size). Below there are some query operators examples.

Press enter or click to view image in full size
Comparison query operator
Press enter or click to view image in full size
Logical query operator
Array query operator

Aggregation stages: way of processing a large number of documents in a collection by passing them through different stages that make a pipeline. As the examples below, the stages can filter, sort, group, reshape and modify documents that pass through the pipeline.

$count stage: count documents in a collection
Press enter or click to view image in full size
$group stage: groups data based on id
Press enter or click to view image in full size
$limit stage: limits the output of documents in a query
Press enter or click to view image in full size
$sort stage: sorts the result of a query in ascending (1) or descending (-1) order // $skip stage: controls the initial output of a query’s results
Press enter or click to view image in full size
Press enter or click to view image in full size
$unwind stage: deconstructs an array field in new documents
Press enter or click to view image in full size
$match stage: filters the documents to return in a query
Press enter or click to view image in full size
$lookup stage: performs a join between data from two collections

Aggregation operators: used in stages to modify documents and perform calculations. They can work with many different expressions, such as: arithmetic, conditional, date and string.

Press enter or click to view image in full size
$rand: returns a random float between 0 and 1
Press enter or click to view image in full size
$round: rounds a number to a whole integer or to a specified decimal place
Press enter or click to view image in full size
$trunc: truncates a number to a whole integer or to a specified decimal place
Press enter or click to view image in full size
$divide: returns the result of dividing the first number by the second. Accepts two argument expressions
Press enter or click to view image in full size
$multiply: multiplies numbers to return the product. Accepts any number of argument expressions
$concat: concatenates any number of strings
Press enter or click to view image in full size
$split: splits a string into substrings based on a delimiter. Returns an array of substrings
$toLower: converts a string to lowercase. Accepts a single argument expression
$toUpper: converts a string to uppercase. Accepts a single argument expression
Press enter or click to view image in full size
$toString: converts value to a string
Press enter or click to view image in full size
$year: returns the year for a date as a number
$sum: returns a sum of numerical values // $avg: returns an average of numerical values
Press enter or click to view image in full size
$cond: ternary operator that evaluates one expression
Press enter or click to view image in full size
$switch: evaluates a series of case expressions

Other methods: in addition to the Find method, there are other methods that can be used to manipulate data stored in MongoDB, such as:

  • updateOne: updates a single document within the collection based on the filter;
  • updateMany: updates all documents that match the specified filter for a collection;
  • findAndModify: updates and returns a single document;
  • findOneAndUpdate: updates a single document based on the filter;
  • findOneAndReplace: replaces a single document based on the filter;
  • findOneAndDelete: deletes a single document based on the filter and sort criteria, returning the deleted document.

In conclusion, the methods, aggregation stages and aggregation operators mentioned above are just part of a large list of options that MongoDB works with.

Get Dalvolinda Silva’s stories in your inbox

Join Medium for free to get updates from this writer.

[Here is the Certificate of Completion.]

--

--

Dalvolinda Silva
Dalvolinda Silva

Written by Dalvolinda Silva

Mainframe Analyst and Developer / Information Scientist