#
Be a Mongoose Master
- MongoDB is an NO-SQL Database, Which store JSON like documents.
- Mongo Stores data like
BSON Format
. - No-SQL databases break from relational models, ideal for managing vast data.
- MongoDB stands out for it's
scalability
,flexibility
andperformance
trusted by giants like Facebook , Google, EBay. - MongoDB is Developed By
MongoDB Inc
- Highly Scalable, High-performance and Open-source.
- Document Oriented Database
- Cost Effective Solutions.
- Rich Ecosystem of Tools, Documents and community.
Features | MongoDB | Relational Database |
---|---|---|
Data Model | Document Oriented | Relational |
Schema | Flexible | RigId |
Scalability | Horizontally & Vertically | Vertically |
Performance | Optimized Structure or UnStructure Data | On Structured Dat |
graph TD;
subgraph A[RDBMS]
subgraph B[Table]
subgraph C[Rows]
D[Columns]
end
end
end
graph TD;
subgraph A[NO-SQL]
subgraph B[Collection]
subgraph C[Document]
D[Fields]
end
end
end
- JSON Like Documents (BSON).
- Indexing
- Aggregation Framework.
- Security Features
- Free Atlas Database
- MongoDB Compass GUI
- Download MongoDB for windows.
- Install mongodb for windows
- When we install mongoDB with
Mongo Compass
automatically installed. -
- Download mongoDB shell msi version
- install It.
- Then go your local drive. My local drive is
C drive
. - Copy this bin folder path:
C:\Program Files\MongoDB\Server\7.0\bin
- Search on window : for
Environment Variable
and setupthen click
on path. - Add a new path
- the save it.
-
- run this command to check
mongosh version
:
mongod --version
- to use
mongoDB shell
onCMD
the command :
mongosh;
- the run any command of mongodb to access database.
- run this command to check
- This help us to data visualization.
- Install any of them and create a connection.
Function | for use | example |
---|---|---|
show dbs |
show all database | show dbs |
use database |
create new or switch | use newDatabaseName |
db.createCollection("name") |
create new collection | db.createCollection('test') |
db.getCollection("collectionName") |
get Collection | db.getCollection('test') |
db.collectionName |
get Collection another way | db.test |
db.collectionName.insertOne(object) |
insert one documents | db.test.insertOne({name: "next Level"}) |
db.collectionName.insertMany([object, object]) |
insert array of data | db.test.insertMany([{name: "next Level"}, {name: "Complete web "}]) |
db.collectionName.find(query) |
find data from collection by query | db.test.find({name: "Complete Web"}) |
db.collectionName.findOne(query) |
find the first Occurrence | db.test.findOne({name: "Complete Web"}) |
-
We can
project
field by usingproject()
or by passing second parameter offind({}, field filter object)
andfindOne(query, field filter object)
-
-
1
means keep the property0
means remove the property. -
syntax: here
db.collectionName.find( {}, { _id: 0, propertyName: 1, propertyName: 1, propertyName: 1 } );
- Example:
// inclusion db.test.find({ company: "Flashspan" }, { name: 1, email: 1, gender: 1 }); // exclusion: db.test.find( { company: "Flashspan" }, { name: 0, email: 0, gender: 0, _id: 0 } );
-
-
-
projection()
andproject()
aren't work forfindOne()
method. -
we can pass
1
or0
as property value. -
1
mean get the property -
0
means remove the property -
syntax :
// inclusion db.test.find({}).project({ name: 1, email: 1 }); db.test.find({ age: 12 }).projection({ name: 1, email: 1, age: 1 }); // exclusion: db.test.find().project({ _id: 0 }); db.test.find().projection({ _id: 0 });
-
-
Operator why use syntax example $eq
to check equal value {<field>: {$eq: value }}
db.test.find({age: {$eq: 12}})
$ne
to check not equal value {<field>: {$ne: value }}
db.test.find({age: {$ne: 12}})
$gt
get the larger value {<field>: {$gt: value }}
db.test.find({age: {$gt: 12}})
$lt
get the smaller value {<field>: {$lt: value }}
db.test.find({age: {$lt: 12}})
$gte
get larger & equal value {<field>: {$gte: value }}
db.test.find({age: {$gte: 12}})
$lte
get smaller & equal value {<field>: {$lte: value }}
db.test.find({age: {$lte: 12}})
$in
get data which matched with array of data {<field>: {$in: [value, value, value] }}
db.test.find({age: {$in: [12, 20, 21, 24]}})
$nin
get data which not matched with array of data {<field>: {$nin: [value, value, value] }}
db.test.find({age: {$nin: [12, 20, 21, 24]}})
-
Operator why use syntax example $and
return data if multiple condition is true {$and: [exp, exp, exp... exp]}
db.test.find({$and: [{age : {$lt: 20} },{'skills.name' : "JAVASCRIPT"},{'skills.name' : "PYTHON"}]}).project({name: 1, age: 1, "skills.name": 1})
$or
if any expression is true return data {$or: [exp, exp, exp... exp]}
db.test.find({$or: [{age : {$lt: 20} },{'skills.name' : "JAVASCRIPT"},{'skills.name' : "PYTHON"}]}).project({name: 1, age: 1, "skills.name": 1})
$not
if any expression is true return data {field:{$not: {operator expression}}}
db.test.find({age: {$not: {$gte: 50}}}).sort({age: 1}).project({age: 1})
$nor
selects the documents that fail all of the query expressions in the array. {field:{$nor: [exp, exp, exp]}}
db.test.find({$nor: [{age: {$in: [7,8,10,12,20,30,40,50,60]}}, {"skills.name": "JAVASCRIPT"}]}).project({age: 1, 'skills.name': 1}).sort({age: 1})
db.test.find({age: {$not: {$gte: 50}}}).sort({age: 1}).project({age: 1})
-
- we can separate every
condition
with comma(,)
. - syntax:
// we write implicit and for same field. {field: {$gt: 12, $lt: 30}} // we write implicit and for query with multiple : {field: value, field: value, filed: value} // it's also implicit $and
- we can separate every
-
- syntax:
{ $and: [expression, expression, expression]; }
- Example:
db.test .find({ $and: [ { age: { $gt: 20 } }, { gender: "Female" }, { "skills.name": "JAVASCRIPT" }, { "skills.name": "PYTHON" }, ], }) .project({ gender: 1, skills: 1 });
-
Example 2:
db.test
.find({
$and: [
{ age: { $gt: 10, $lt: 30 } },
{ gender: "Male" },
{ interests: { $in: ["Gaming", "Reading"] } },
{ "skills.name": { $in: ["JAVASCRIPT"] } },
{ "skills.name": { $in: ["C#"] } },
],
})
.project({
age: 1,
gender: 1,
interests: 1,
skills: 1,
});
-
- We can explicitly use
$or
operator - syntax:
{ $or: [expression, expression, expression]; }
- Example:
// find the user who have skills on Python or Javascript. db.test .find({ $or: [{ "skills.name": "PYTHON" }, { "skills.name": "JAVASCRIPT" }], }) .project({ skills: 1, });
- We can explicitly use
-
$in
operator implicitly worked like$or
operator.
db.test.find({ age: { $in: [10, 20, 30, 40] } });
- We can update any normal object with
$set
Operator. - When we need to update property element which is an array of object.
- The element stored on any position.
- To find out the element we can use
property.$.propertyName
: value - Example:
db.another_testing.updateOne( { _id: ObjectId("6550655ec31abae6aaa3419a"), skills: { $elemMatch: { name: "C#" } }, }, { $set: { "skills.$.level": "Expert", }, } );