- NoSQL database
- JSON documents (BSON)
- Flexible structure to store the data
- Large amount of data can be easily managed
| SQL | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Column | Field |
| Row | Document |
| Foreign Key | Sub Document (Reference) |
- Read document
{ "name" : "Tiago" }
- AND query
{ $and : [ { "name" : "test" }, { "mail" : "test@gmail.com" } ] }
- OR query
{ $or : [ { "name" : "Tiago" }, { "mail" : "tiago@gmail.com" } ] }
- IN query
{ "name" : { $in : [ "Tiago", "Test" ] } }
- Get documents by sub document field
{ "department.department_name" : "Computer Science" }
- Get documents by array value
{ "subjects.subject_name" : "Java" }
- Like Query
{ "mail" : /gmail/ }
- Start With Query
{ "name" : /^Tiago/ }
- Update document (Shell)
One document -> update() or updateOne()
Many documents -> updateMany()
db.student.update(
{
"name": "Tiago"
},
{
$set: {
"mail": "tiago@gmail.com"
}
}
)
- Delete document (Shell)
db.student.remove(
{
"name": "Tiago"
}
)
- Find query (Shell)
db.student.find(
{
"name": "Tiago"
}
)
Formula: (Page No -1) * Page Size
| Page No | Skip | Limit (Page Size) |
|---|---|---|
| 1 | 0 | 10 |
| 2 | 10 | 10 |
| 3 | 20 | 10 |
- Patient (without relations)
POST - /api/patients
{
"name": "Tiago",
"email": "tiago@hotmail.com",
"address": {
"locality": "Canidelo"
}
}
- Student (with relations)
POST - /api/students
{
"name": "Tiago",
"email": "Tiago@gmail.com",
"department": {
"departmentName": "Computer Science"
},
"subjects": [
{
"subjectName": "JAVA",
"marksObtained": 80
}
]
}
MongoDB Demo App deployed at Heroku using MongoDB Cloud