MongoDB Query language

Outline

MongoDB query language

  • MongoDB query language is based on a concept of pattern matching
  • A query is expressed as a BSON pattern and all documents that match the pattern are included in an answer
  • A method find() can be used to match a pattern with the documents in a collection orders
      db.orders.find({"_id":"ALFKI"})
  • Matching of an empty pattern { } with a collection orders returns an entire collection
      db.orders.find({})
  • Finding the first 3 documents in a collection orders
      db.orders.find({}).limit(3)
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

A sample database

  • A conceptual schema of a database with information about suppliers, products, customers, orders, and details of orders

      Logical schema

In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

A sample database

  • A sample collection orders

      Logical schema

In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

A sample database

  • A sample document, that belongs to a class CUSTOMER
      {
              "_id" : "ALFKI",
              "CUSTOMER" : {
                       "customer code" : "ALFKI",
                       "company name" : "Alfreds Futterkiste",
                       "contact name" : "Maria Anders",
                       "contact title" : "Sales Representative",
                       "address" : "Obere Str. 57",
                       "city" : "Berlin",
                       "region" : null,
                       "postal code" : "12209",
                       "country" : "Germany",
                       "phone" : "030-0074321",
                       "fax" : "030-0076545",
                       "submits" : [ ]
              }
      }
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

A sample database

  • A sample nested document, that belongs to a class CUSTOMER
      {"_id" : "FAMIA",
       "CUSTOMER" : {
                     "customer code" : "FAMIA",
                      ...  ...  ...  ...  ...  ...
                     "submits" : [
                                  {
                                   "ORDER" : {
                                              "order id" : 328,
                                               ...  ...  ...  ...  ...
                                              "consists of" : [
                                                               {
                                                                "ORDER DETAIL" : {
                                                                                  "product name" : "Louisiana Fiery Hot Pepper Sauce",
                                                                                  ...  ...  ...  ...  ...
                                                                                 }
                                                               },
                                                               {
                                                                "ORDER DETAIL" : {
                                                                                  "product name" : "Raclette Courdavault",
      	                                                                    ...  ...  ...  ...  ...
                                                                                 }
                                                               }
                                                              ]
                                             }
                                  }
      	                   ]
      	      }	}
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

A sample database

  • A sample nested document, that belongs to a class SUPPLIER
      {
       "_id" : "Karkki Oy",
       "SUPPLIER" : {
                     "company name" : "Karkki Oy",
                     "contact name" : "Anne Heikkonen",
                     "contact title" : "Product Manager",
                     "address" : "Valtakatu 12",
                     ...  ...  ...  ...  ...  ...
                     "supplies" : [
                                   {
                                    "PRODUCT" : {
                                                 "product name" : "Maxilaku",
                                                 "category name" : "Confections",
                                                  ...  ...  ...  ...  ... 
                                                }
                                   },
                                   {
                                    "PRODUCT" : {
                                                 "product name" : "Valkoinen suklaa",
                                                 "category name" : "Confections",
                                                 ...  ...  ...  ...  ...
                                                }
                                   }
      
                                  ]
                    }
      }
      
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Simple queries

  • Find total number of documents in a collection
      db.orders.count()
  • Find all information about all customers
      db.orders.find({"CUSTOMER":{$exists:true}})
  • Find all information about all suppliers
      db.orders.find({"SUPPLIER":{$exists:true}})
  • Find all information about the customers living in Germany
      db.orders.find({"CUSTOMER.country":"Germany"})
  • Find all information about the suppliers living in a city of Oviedo
      db.orders.find({"SUPPLIER.city":"Oviedo"})
  • Find all information about the suppliers who live in the Netherlands and have a contact title Accounting Manager
      db.orders.find({"SUPPLIER.country":"Netherlands",
      	        "SUPPLIER.contact title":"Accounting Manager"})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Queries with Boolean operations

  • Comparison "key"="value"
      {"key":"value"}
      {"key":{$eq:"value"}}
  • Comparison "key" > "value"
      {"key":{$gt:"value"}}
  • Disjunction ("key1"="value1") or ("key2"="value2")
      {$or:[{"key1":"value1"},{"key2":"value2"}]}
  • Conjunction ("key1"="value1") and ("key2"="value2")
      {$and:[{"key1":"value1"},{"key2":"value2"}]}
  • Boolean expression (("key1"="value1") or ("key2"="value2")) and ("key3"="value3")
       	      
      {$and:[{$or:[{"key1":"value1"},{"key2":"value2"}]},{"key3":"value3"}]}
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries with Boolean operations

  • Negation of a comparison "key" not ="value"
       
      {"key":{$not:{$eq:"value"}}}
  • Negation of an expression not (("key1"="value1") or ("key2"="value2"))
       
      {$nor:[{"key1":"value1"},{"key2":"value2"}]}
  • Negation nor ("key1"="value1")
       
      {$nor:[{"key1":"value1"}]}
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries with Boolean operations

  • Find all information about the suppliers who live in the Netherlands and have a contact title Accounting Manager
       
      db.orders.find({$and:[{"SUPPLIER.country":"Netherlands"},
      	              {"SUPPLIER.contact title":"Accounting Manager"}]})
  • Find all information about the suppliers who live in the Netherlands or have a contact title Accounting Manager
       	    
      db.orders.find({$or:[{"SUPPLIER.country":"Netherlands"},
      	             {"SUPPLIER.contact title":"Accounting Manager"}]})
  • Find all information about the customers who live in France or in Germany
       
      db.orders.find({"CUSTOMER.country":{$in:["France","Germany"]}})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries with Boolean operations

  • Find all information about the customers who do not live in Germany
       
      db.orders.find({"CUSTOMER.country":{$not:{$eq:"Germany"}}})
  • Find all information about the customers who do not both live in Netherlands or have a contact title Accounting Manager
  • Find all information about the customers who do not live in Netherlands and do not have a contact title Accounting Manager
      db.orders.find({$nor:[{"SUPPLIER.country":"Netherlands"},
      	       {"SUPPLIER.contact title":"Accounting Manager"}]})
  • Find all information about the customers who do not live in Germany
       
      db.orders.find({$nor:[{"CUSTOMER.country":"Germany"}]})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Queries on nested documents

  • A sample collection orders

      Logical schema

In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries on nested documents

  • Find all information about suppliers who supply a product named Laughing Lumberjack Lager
       
      db.orders.find({"SUPPLIER.supplies.PRODUCT.product name":"Laughing Lumberjack Lager"})
  • Find all information about suppliers living in London who supply a product named Chai
       
      db.orders.find({$and:[{"SUPPLIER.city":"London"},
                            {"SUPPLIER.supplies.PRODUCT.product name":"Chai"}]})
  • Find all information about suppliers living in London who supply a product named Chai or a product named Chang
       
      db.orders.find({$and:[{"SUPPLIER.city":"London"},
                            {$or:[{"SUPPLIER.supplies.PRODUCT.product name":"Chai"},
                                  {"SUPPLIER.supplies.PRODUCT.product name":"Chang"}]}]})
      db.orders.find({$and:[{"SUPPLIER.city":"London"},
                            {"SUPPLIER.supplies.PRODUCT.product name":{$in:["Chai","Chang"]}}]})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries on nested documents

  • Find all information about suppliers living in London who supply a product named Chai and a product named Chang
      db.orders.find({$and:[{"SUPPLIER.city":"London"},
                            {"SUPPLIER.supplies.PRODUCT.product name":"Chai"},
                            {"SUPPLIER.supplies.PRODUCT.product name":"Chang"}]})
  • Find all information about suppliers who supply at least one product
       
      db.orders.find({$and:[{"SUPPLIER.supplies":{$exists:true}},
                            {"SUPPLIER.supplies":{$ne:[]}}]})
  • Find all information about suppliers who do not supply any products
       
      db.orders.find({$and:[{"SUPPLIER.supplies":{$exists:true}},
                           {"SUPPLIER.supplies":{$eq:[]}}]})
  • Find all information about customers who submitted an order for at least one product Flotemysost
       
      db.orders.find({"CUSTOMER.submits.ORDER.consists of.ORDER DETAIL.product name":"Flotemysost"})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Queries on arrays

  • Array equal to [1,2,3,4,5]
      {"array":{$all:[1,2,3,4,5]}}
  • Array includes an element that satisfies a condition
      {"array":{$elemMatch:{$eq:2}}}
      {"array":{$elemMatch:{$gt:2,$lt:4}}}
  • Array includes a document satisfies a condition
      {"array":{$elemMatch:{"key":{$eq:2}}}}
      {"array":{$elemMatch:{"key":{$gt:2,$lt:4}}}}
  • Size of an array
      {"array":{$size:5}}
      {"array":{$size:0}}
      {"array":[]}
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries on arrays

  • Find information about all customers who submitted an order that contains only a product Boston Crab Meat at unit price 14.7 in quantity 20 with discount equal to 0
      db.orders.find({"CUSTOMER.submits.ORDER.consists of":
      	                          {$eq:[{"ORDER DETAIL":{"product name":"Boston Crab Meat",
                                                               "unit price":14.7,
                                                               "quantity":20,
                                                               "discount":0}}]}})
                
  • Find information about all suppliers such that their second supplied product is named Chang
       
      db.orders.find({"SUPPLIER.supplies.1.PRODUCT.product name":"Chang"})
  • Find information about all suppliers such that their first supplied product is named Chai and the second supplied product is named Chang
       
      db.orders.find({$and:[{"SUPPLIER.supplies.0.PRODUCT.product name":"Chai"},
                            {"SUPPLIER.supplies.1.PRODUCT.product name":"Chang"}]})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries on arrays

  • Find information about all customers who purchased at least one product with discount greater than 0.2
       
      db.orders.find({"CUSTOMER.submits.ORDER.consists of.ORDER DETAIL.discount":{$gt:0.2}})
       
      db.orders.find({"CUSTOMER.submits.ORDER.consists of":{$elemMatch:{"ORDER DETAIL.discount":{$gt:0.2}}}})
  • Find information about all customers who purchased at least one product with discount equal to 0.25 and quantity equal to 16
       
      db.orders.find({"CUSTOMER.submits.ORDER.consists of":{$elemMatch:{"ORDER DETAIL.discount":{$eq:0.25},
      	                                                          "ORDER DETAIL.quantity":{$eq:16}}}})
  • Find information about all customers who purchased 4 products in one order
       
      db.orders.find({"CUSTOMER.submits.ORDER.consists of":{$size:4}})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Queries on arrays

  • Find information about all customers who purchased more than 4 products in one order
       
      db.orders.find({"CUSTOMER.submits.ORDER.consists of.4":{$exists:true}})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Projections

  • A sample collection orders

      Logical schema

In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Projections

  • Find only a company name and contact name for all suppliers
       
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.company name":1,"SUPPLIER.contact name":1})
  • Find all information about suppliers except a company name, contact name and _id
       
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.company name":0,"SUPPLIER.contact name":0})
  • Find all information about suppliers except products supplied by suppliers and _id
       
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.supplies":0}))
  • Find only information about products supplied by suppliers
       
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.supplies":1}))
       
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.supplies.PRODUCT":1}))
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Projections

  • Find only the names of products supplied by suppliers
        
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.supplies.PRODUCT.product name":1})
  • Find only the names of products and categories of products supplied by suppliers
        
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.supplies.PRODUCT.product name":1,
      	                                            "SUPPLIER.supplies.PRODUCT.category name":1})
  • Find only company names of suppliers and the names of products supplied by suppliers
        
      db.orders.find({"SUPPLIER":{$exists:true}},{"_id":0,"SUPPLIER.company name":1,
                                                          "SUPPLIER.supplies.PRODUCT.product name":1})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Queries about nulls and missing keys

  • Find all information about the customers who have no region information
       
      db.orders.find({"CUSTOMER.region":null})
  • Find all information about the customers who have region information
       
      db.orders.find({"CUSTOMER.region":{$not:{$eq:null}}})
  • Find all information about the customers who have PO Box in their description
       
      db.orders.find({"CUSTOMER.PO Box":{$exists:true}})
  • Find all information about the customers who do not have PO Box in their description
       
      db.orders.find({"CUSTOMER.PO Box":{$exists:false}})
      db.orders.find({"CUSTOMER.PO Box":{$not:{$exists:true}}})
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Query language

Outline

Iterations over a cursor

  • Create a cursor and display all information about suppliers
       
      var cursor = db.orders.find({"SUPPLIER":{$exists:true}})
      while(cursor.hasNext())
      { print(tojson(cursor.next())); }
      var cursor = db.orders.find({"SUPPLIER":{$exists:true}})
      cursor.forEach(printjson)
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

References