MongoDB Iceberg: Introduction to MongoDB Query.

ADDI Kamal
6 min readDec 3, 2021

In the last tutorial “MongoDB Iceberg: How to install MongoDB on Ubuntu Server?”, you have installed MongoDB on an Ubuntu 20.04 server and learned how to manage it as a systemd service. In this tutorial, you will learn how to import data to MongoDB and query your database with the language proposed by MongoDB using the mongo shell.

MongoDB Query Document Using Find Method

Before we start, download the following dataset “restaurants.json”.

To start the shell, run the mongo executable :

  • List of MongoDB commands : db.help()

This command will help us discover all the commands that can be used in MongoDB.

> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.commandHelp(name) returns the help for the command
db.createUser(userDocument)
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.currentOp() displays currently executing operations in the db
db.dropDatabase(writeConcern)
db.dropUser(username)
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSecondaryOk() allow queries on a replication secondary server
db.getName()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.hello() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSecondaryReplicationInfo()
db.rotateCertificates(message) - rotates certificates, CRLs, and CA files and logs an optional message
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setVerboseShell(flag) display extra information in shell output
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.shutdownServer()
db.stats()
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.version() current version of the server
db.watch() - opens a change stream cursor for a database to report on all changes to its non-system collections.
>

To display the databases you have in your MongoDB server, type : show dbs

To exit the shell, type exit or use the Ctrl+C shortcut.

Go to the ~/data directory by issuing the following command : cd ~/data/restaurants

To import our sample data from “restaurants.json” into the MongoDB server, type:

mongoimport --db restaurants --collection restaurants --file restaurants.json

Start the shell and type show dbsagain :

Now you should see the “restaurants” database.

To switch to the restaurants database type : use restaurants

To verify that your database is now restaurants, type db :

Show collections inside the database by entering the command : show collections

You should see one collection called: “restaurants

To count the number of all documents in the “restaurants” collection, use the following operation: db.restaurants.count()

To see what a document from our restaurant collection looks like, use the “findOne()” function: db.restaurants.findOne()

{
"_id" : ObjectId("6196299f64ad3534a8970ab2"),
"address" : {
"building" : "469",
"coord" : {
"type" : "Point",
"coordinates" : [
-73.961704,
40.662942
]
},
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 8
},
{
"date" : ISODate("2014-07-01T00:00:00Z"),
"grade" : "B",
"score" : 23
},
{
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2012-05-08T00:00:00Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}

Each restaurant has an address (nested document, with GPS coordinates, street, and postal code), a district “borough”, the type of cuisine, a set of notes (inspection results), a name, and a restaurant id.

  • Filtering :

Let’s start with a simple type of query: “filtering”. For that, we will use the “find()” function. The following query retrieves all the restaurants in the borough of “Brooklyn”.

db.restaurants.find({"borough" : "Brooklyn"})

The results look like this:

To count, just add the “count()” function :

6085 restaurants returned.

Now we are looking among these restaurants for those that do Italian cuisine.

db.restaurants.find(
{ "borough" : "Brooklyn",
"cuisine" : "Italian" }
)
  • Filtering with operations :

Filtering by exact value is not sufficient to express everything you would like to find in the collection. We are now looking for the names and scores of restaurants in Manhattan with a score less than 10. We can use arithmetic operators on the keys (numeric value). For this, it will be prefixed with a dollar “$” plus “lt”(less than), so the operator is “$lt”.

db.restaurants.find(
{"borough":"Manhattan",
"grades.score":{$lt : 10}
}
)

Output :

What is confusing about this result is that we find scores greater than 10!

The operation “grades.score”: {“$lt”: 10}, means:

  • Does the “grades” list contain a score (at least) with a value less than 10?

And indeed, there is a score of “2” respecting the criteria.

If we want to return only those who do not have a score greater than 10, we must then combine the first operation with a negation of the condition “≥10”. The condition is then checked on each element of the list.

db.restaurants.find(
{"borough":"Manhattan",
"grades.score":{
$lt:10,
$not:{$gte:10}
}
},
)
  • Comparison & Logical Operators :
     ╔══════════╦═════════╦═════════════════════════════════╗
║ $gt ║ > ║ greater than ║
║ $gte ║ ≥ ║ greater than or equal ║
║ $lt ║ < ║ less than ║
║ $lte ║ ≤ ║ less than or equal ║
║ $eq ║ = ║ equal ║
║ $ne ║ ≠ ║ not equal ║
║ $in ║ ∈ ║ in (in a list) ║
║ $nin ║ ∉ ║ not in ║
║ $exists ║ ∃ ║ the key exists in the document ║
║ $and ║ & ║ logical AND ║
║ $or ║ V ║ logical OR ║
║ $not ║ ¬ ║ negation ║
║ $nor ║ V ║ logical OR ║
╚══════════╩═════════╩═════════════════════════════════╝

Conclusion :

In this tutorial, we learn how to import data to MongoDB and how to query your database using the mongo shell.

I hope this tutorial was useful for you.

Best Regards,

Kamal ADDI

--

--

ADDI Kamal

Hi there, I’m Kamal, a data engineering student at the National Institute of Posts and Telecommunications. passionate about data science, big data, and IA.