MongoDB Databases, Collections, Documents

Outline

Basics

  • MongoDB is a database system that belong to a class of so called NoSQL database systems based on a data model different from the relational model and data definition, manipulation, retrieval, and administration languages different from SQL
  • MongoDB data model (BSON) is based a on concept of key:value pairs grouped into documents and arrays
  • MongoDB database system operates on a number of databases
  • A MongoDB database is a set of collections
  • A MongoDB collection is a set of documents
  • A MongoDB document is a set of key:value pairs
  • A MongoDB value is either an atomic value or a document or an array
  • A MongoDB atomic value is of one of the types included BSON specification like number, string, date, and so on
  • A MongoDB array is a sequence of values
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Basics

  • Each MongoDB key:value pair must have a unique key within document
  • Each MongoDB document must have a unique identifier within a collection
  • Each MongoDB collection must have a unique name within a database
      
      {"_id": ObjectId(),
       "full name": {"first name":"James",
                     "initials":null,
                     "last name":"Bond"},
       "employee number":"007",
       "skills":["cooking", "painting", "gardening"],
       "cars owned":[ {"rego":"007-1",
                       "made":"Porsche"},
                      {"rego":"007-2",
                       "made":"Ferrari"} ],
       "secret codes":[ [1,2,3,4],
                        [9,8,7,5] ],
       "date of birth":new Date("1960-01-01")
      }
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Databases, Collections, Documents

Outline

Architecture

  • MongoDB flexible storage architecture automatically manages the movement of data between storage engine technologies using native replication
  • MongoDB stores data as documents in a binary representation called BSON (Binary JSON)
  • MongoDB query model is implemented as methods or functions within the API of a specific programming language, as opposed to a completely separate language like SQL
  • MongoDB provides horizontal scale-out for databases on low cost, commodity hardware or cloud infrastructure using a technique called sharding, which is transparent to applications
  • In-Memory storage engine enables performance advantages of in-memory computing for operational and real-time analytics workloads
  • MongoDB Enterprise Advanced provides extensive capabilities to defend, detect, and control access to data (data security)
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Architecture

  • MongoDB Ops Manager makes easy for operation teams to deploy, monitor, backup and scale the system (system management)
  • MongoDB Atlas provides all of the features of Database as a Service cloud computing model
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Databases, Collections, Documents

Outline

Server

  • The present installation of MongoDB version 8.2.2 on Ubuntu running on a virtual machine available from Moodle is fully automated
  • To start MongoDB server process the following command in Terminal window:
      sudo systemctl start mongod
  • To check a status of MongoDB server process the following command in Terminal window:
      sudo systemctl status mongod
  • To stop MongoDB server process the following command in Terminal window:
      sudo systemctl stop mongod
  • To start MongoDB command line shell process the following command in Terminal window:
      mongosh
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Server

  • Getting the first help from MongoDB shell
      
      help
      
      db.help()        help on db methods
      show dbs         show database names
      show collections show collections in current database
      use db_name      set current database
      ... ... ...      ... ... ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Databases, Collections, Documents

Outline

Databases

  • Setting a default database
      
      use database-name
  • For example using a database local
      
      use local
  • Creating and switching to a new database mydb
      
      use mydb
  • Listing the databases
      
      show dbs
      
      local 0.000GB
      mydb 0.000GB
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Databases, Collections, Documents

Outline

Collections

  • Creating a new collection with an empty document
      
      db.mycol.insert({})
  • Listing the contents of a collection
      
      db.mycol.find()
      { "_id" : ObjectId("57e385f8ffc660a351b58010") }
  • Listing the collections
      
      show collections
      mycol
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Databases, Collections, Documents

Outline

Documents

  • Creating a new non empty document
      
      db.mycol.insert({"one":"1", "many ones":[1,1,1,1]})
  • Listing the contents of a collection
      
      db.mycol.find()
      { "_id" : ObjectId("57e385f8ffc660a351b58010") }
      { "_id" : ObjectId("57e38cbeffc660a351b58012"),
        "one" : "1", "many ones" : [ 1, 1, 1, 1 ] }
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

MongoDB Databases, Collections, Documents

Outline

Simple DDL and DML

  • Removing all documents from a collection
      
      db.mycol.remove({})
  • Removing a collection
      
      db.mycol.drop()
  • Removing a database
      
      db.dropDatabase()
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Simple DDL and DML

  • Let a file dbload.js contains the following insert methods
      
      db.mycol.insert({"CITY": {"name":"Wollongong",
                                "population":"80K",
                                "country":"Australia",
                                "state":"New South Wales"} });
      db.mycol.insert({"EMPLOYEE":{"enum":1234567,
                                   "full-name":"Janusz R.Getta",
                                   "salary": "200K",
                                   "hobbies":["cooking",
                                              "painting",
                                              "gardening"]} });
  • Processing a script inserts two documents into a collection mycol
      
      load("dbload.js")
  • Listing a collection mycol
      
      db.mycol.find()
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Simple DDL and DML

      {
            "_id" : ObjectId("57e3c817fe6a1bfd5105022a"),
            "CITY" : {
                      "name" : "Wollongong",
                      "population" : "80K",
                      "country" : "Australia",
                      "state" : "New South Wales"
                     }
      }
      {
            "_id" : ObjectId("57e3c817fe6a1bfd5105022b"),
            "EMPLOYEE" : {
                          "enum" : 1234567,
                          "full-name" : "Janusz R.Getta",
                          "salary" : "200K",
                          "hobbies" : [
                                       "cooking",
                                       "painting",
                                       "gardening"
                                      ]
                         }
      }
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

References

  • MongoDB Architecture
  • Banker K., Bakkum P., Verch S., Garret D., Hawkins T., MongoDB in Action, 2nd ed., Manning Publishers, 2016
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026