MongoDB
...

Introduction
...

MongoDB is a popular open-source, document-oriented NoSQL database. Instead of storing data in tables like traditional relational databases, MongoDB stores structured data in JSON-like documents.

Key Features
...

  • Document-oriented - stores data in flexible, JSON-like documents instead of rows and columns
  • Scalability - easy to scale horizontally with automatic sharding
  • High performance - performs well with large datasets and heavy workloads
  • High availability - supports replication and failover for high availability
  • Flexible data model - dynamic schemas allow you to store different data in the same collection
  • Indexing - indexes support faster queries and can include keys from embedded documents and arrays

Components
...

The main components of MongoDB are:

  • mongod - The MongoDB daemon that does the core data storage
  • mongos - The query router that provides sharding
  • Documents - Data records composed of field-value pairs
  • Collections - Groupings of MongoDB documents
  • Databases - Container for collections

Basic Concepts
...

  • JSON-like documents contain field-value pairs
  • Collections contain sets of documents
  • Databases contain collections
  • Uses BSON format to store documents (binary JSON)

Syntax
...

Data Bases
...

  • show data bases: show dbs
    • if you have a data base which is empty the show dbs command won't show the empty data base.
  • switch to data base : use 'data base name'
  • create data base : use 'name of the data base you want'
  • create collection : db.createCollection("name")
  • drop data base : db.dropDatabase()

Insert
...

  • to insert a document : db.students.insertOne({name:"gholi", age:30, gpa:2.5})
  • to return all documents in a collection : db.students.find()
  • add multiple documents : db.students.insertMany([{name: "gholi", age: 38, gpa: 2.5}, {name: "gholam", age: 39, gpa: 3.5}, {name: "ghodrat", age: 40, gpa: 4.5}])

Data Types
...

db.students.insertOne({
name:"gholi", //string
age:32, //integer
gpa:2.8, // double
fullTime: false, // boolean
registerDate: new Date(), // date
gradDate: null, // null
courses: ["chemistry", "physics", "calculus"], // array
addres: {street:"fake street",
		 city: "gholi abad",
		 zip: 12345}}) // nested documents

Sorting and Limiting
...

Sorting
...

  • in alphabetical order : db.students.find().sort({name:1})
  • in reverse alphabetical order : db.students.find().sort({name:-1})
  • in ascending order : db.students.find().sort({gpa:1})
  • in descending order : db.students.find().sort({gpa:-1})

Limiting
...

  • limiting by number of documents from top of the list : db.students.find().limit(3) which returns top 3 documents.

Sort and Limit combined :
...

db.students.sort({gpa:-1}).limit(1) this query returns the student with highest gpa.

Find
...

  • find any students named "gholi" : db.students.find({name:"gholi"})
    • you can add multiple filters : db.students.find({name:"gholi", gpa: 4.0})
  • find every students but only show their name : db.students.find({}, {name:true})
    • disable id's : db.students.find({}, {_id:false, name:true})

Update
...

  • update one data : db.students.updateOne(filter, update)
  • adding / changing a full time field : db.students.updateOne({name:"gholi"}, {$set:{fulltime:true}})
  • remove field : db.students.updateOne({name:"gholi"}, {$unset:{fulltime:""}})
    • to remove a field just set to an empty string.
  • update multiple : db.students.updateMany({name:"gholi"}, {$set:{fulltime:false}})
  • remove multiple : db.students.updateMany({name:"gholi"}, {$set:{fulltime:false}})
  • check if a field exist : db.students.updateMany({fullTime:{$exists:false}}, {$set:{fulltime:true}})

Delete
...

  • delete one document : db.students.deleteOne({name:"gholi"})
  • delete multiple document : db.students.deleteMany({name:"gholi"})
  • delete if something doesn't exist : db.students.deleteMany({registerDate:{$exist:false}})

Comparison Operators
...

  • the not(!) operator : $ne
    • example to find anyone but "gholi" : db.students.find({name:{$ne:"gholi"}})
  • less than(<) operator : $lt
    • example to find people under 20 yo : db.students.find({age:{$lt:20}})
    • less than or equal : $lte
    • greater than : $gt
    • greater than or equal : $gte
  • multiple operators : db.students.find({gpa:{$lt:4, $gte:3}})
  • in operator : $in
    • example : db.students.find({age:{$in:["gholi", "ghodrat"]}})
  • not in operator : $nin
    • example : db.students.find({age:{$nin:["gholi", "ghodrat"]}})

Logical Operators
...

  • and operator : $and
    • example : db.students.find({$and: [{fullTime: true}, {age:{$lte:22}}]})
  • or operator : $or
    • example : db.students.find({$or: [{fullTime: true}, {age:{$lte:22}}]})
  • nor operator : $nor
    • example : db.students.find({$nor: [{fullTime: true}, {age:{$lte:22}}]})
  • not operator : $not
    • db.students.find({age:{$not:{$gte:30}}})

Indexes
...

  1. Indexes help MongoDB perform queries faster by sorting documents in the collection on a specific field or set of fields.
  2. Indexes are created on one or more fields of a collection to improve the speed of find(), sort() and grouping operations.
  • how to make alphabetical index for a field : db.students.createIndex({name: 1})
  • get Indexes : db.students.getIndex()
  • drop Index by using name of the Index : db.students.getIndex("name_1")

Collections
...

  • create Collections : db.createCollection("teachers")
  • set max size to a collection : db.createCollection("teachers", {capped:true, size:10000000, max:100}) this means the size is at most 10 megabytes and maximum number of documents can be 100.
  • you can set indexes to false so the insert and other operations are faster : db.createCollection("teachers", {capped:true, size:10000000, max:100}, {autoIndexId: false})