My simplified version of mongodb concepts

MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. Instead of tables and rows, MongoDB stores data in key-value pairs. It is is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. It works on concept of collection and document. 

Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.

A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

Below is the image to compare RDBMS Vs MongoDB










Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB, there is no concept of relationship.

Advantages of MongoDB over RDBMS:

  • Schema less − MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another.
  • Structure of a single object is clear.
  • No complex joins.
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
  • Tuning.
  • Ease of scale-out − MongoDB is easy to scale.
  • Conversion/mapping of application objects to database objects not needed.
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data.
Why we go for MongoDB?

  1. Document Oriented Storage − Data is stored in the form of JSON style documents.
  2. Index on any attribute
  3. Replication and high availability
  4. Auto-Sharding (horizontal scaling)
  5. Rich queries
  6. Fast in-place updates
Where to Use MongoDB?
  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub
MongoDB provides two types of data models: Embedded data model and Normalized data model. Based on the requirement, we can use either of the models while preparing your document.

Embedded Data Model: In this model, we can have (embed) all the related data in a single document, it is also known as de-normalized data model.
Eg:
{
_id: ,
Emp_ID: "10025AE336"
Personal_details:{
First_Name: "ABC",
Last_Name: "DEF",
Date_Of_Birth: "1995-09-26"
},
Contact: {
e-mail: "abc@gmail.com",
phone: "332232432"
},
Address: {
city: "Cypress",
Area: "Orange",
State: "CA"
}
}

Normalized Data Model: In this model, you can refer the sub documents in the original document, using references. For example, we can re-write the above document in the normalized model as:
Eg:
Employee:
{
_id: <ObjectId101>,
Emp_ID: "10025AE336"
}
Personal_details:
{
_id: <ObjectId102>,
empDocID: " ObjectId101",
First_Name: "ABC",
Last_Name: "DEF",
Date_Of_Birth: "1995-09-26"
}
Contact:
{
_id: <ObjectId103>,
empDocID: " ObjectId101",
e-mail: "abc@gmail.com",
phone: "332232432"
}
Address:
{
_id: <ObjectId104>,
empDocID: " ObjectId101",
city: "Cypress",
Area: "Orange",
State: "CA"
}


Basic commands to run on MongoDB after installation:
To run the server:
C:\>mongod -f C:\Users\urspv\Documents\Pega\MongoDB\mongoData\mongo.conf (run the mongodb server)
After server running, open another command line and type the command like C:\>mongo
Type the below mongo commands after ">"
1) show dbs  ==> list of DB's
2) use demodb  ==> create a DB with a name of demodb [db.demodb.insert({"name":"sampledb"}) ==> To display database, you need to insert at least one document into it]
3) db.help() ==> To list all the MongoDB commands
4) db.stats() ==> details of databases along with several collections and related parameters of that Database
Eg:
{
        "db" : "demoDB",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}
5) cls  ==> to clear the screen
6) db ==> to know the current DB where we are
7) db.dropDatabase() ==> drop the DB
8) db.createCollection(Name,Options) ==> create a collection(DB table)
9) show collections ==> to show all the collections(DB tables)
10) db.createCollection(Name,{capped : true, size : sizeLimit , max : documentLimit }) ==> Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read, and delete operations.
Eg: db.createCollection("EmpColl",{capped:true,size:2000}) ==> This will create an Employee collection
11) db.collectionName.drop() ==> To drop the collection(DB table)
Eg: db.demoCollection.drop()
12) db.collectionName.insertMany() ==> To add the documents(rows) to the collection(table)
Eg:
db.EmpColl.insertMany([{"Empid":"1","Name":"Ravi","Email":"ravi@test.com"},{"Empid":"2","Name":"Pisupati","Email":"kumar@test.com"},{"Empid":"3","Name":"Bobby","Email":"bobby@test.com"}])
Result:
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5ffd206d4f3dfd202ccc5ea3"),
                ObjectId("5ffd206d4f3dfd202ccc5ea4"),
                ObjectId("5ffd206d4f3dfd202ccc5ea5")
        ]
}
13) db.collectionName.insertOne() ==> to insert a single document(record)
Eg:
db.EmpColl.insertOne({"Empid":"4","Name":"Pisu","Email":"pisu@test.com"})
Result:
{
        "_id" : ObjectId("5ffd2a494f3dfd202ccc5ea9"),
        "Empid" : "4",
        "Name" : "Pisu",
        "Email" : "pisu@test.com"
}
14) db.collectionName.find().pretty() ==> find the documents(rows) within the “EmpColl” collection
Eg:
db.EmpColl.find().pretty() 
Result:
{
        "_id" : ObjectId("5ffd21c34f3dfd202ccc5ea6"),
        "Empid" : "1",
        "Name" : "Ravi",
        "Email" : "ravi@test.com"
}
{
        "_id" : ObjectId("5ffd21c34f3dfd202ccc5ea7"),
        "Empid" : "2",
        "Name" : "Pisupati",
        "Email" : "kumar@test.com"
}
{
        "_id" : ObjectId("5ffd21c34f3dfd202ccc5ea8"),
        "Empid" : "3",
        "Name" : "Bobby",
        "Email" : "bobby@test.com"
}
15) db.collectionName.updateOne({KeyToUpdate},{Set Command}) ==> To update a single document and is not applicable for capped collections
Eg: db.Sample.updateOne({"Empid" : "1"},{$set : {"Email" : "ravi@test123.com"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
16) db.collectionName.updateMany({filter},{Set Command}) ==> To update many documents(rows)
Eg: db.Sample.updateMany( { "Empid" : { $lt: 3 } },{ $set: { "Email": "ravi@test1234.com"} } )
17) db.collectionName.deleteOne({DeletionCondition}) ==> To delete the document(row)
Eg: db.Sample.deleteOne({"Empid":"4"})
{ "acknowledged" : true, "deletedCount" : 1 }
18) db.collectionName.distinct(field) ==> to get unique records
Eg: db.Sample.distinct("Empid")
[ "1", "2", "3" ]
19) db.collectionName.renameCollection(newCollectionName) ==> To rename the collection(table) with new name

To avoid running the above commands on the command line, we have few GUI tools available to work on this DB. RoboMongo is one good example and you can download it from google.

The community edition runs on the localhost:27017 and we can connect to it using the DB editor and also we can build the programming logic(integration) to connect to this DB.

The next topic is to connect this DB from Pega PE and play with the DB. For more details, you can search my blog.

References:
1) google.com
2) mongodb site

Popular posts from this blog

Connecting Claude to Pega Infinity 25.1.3 via MCP — Step-by-Step

itextpdf API to generate PDF doc from an image file using Pega PE

Understanding of Hugging Face platform for AI/ML platform