[Link]
htm
[Link]
[Link]
first install mongodb
C:\Program Files\MongoDB\Server\5.0\bin ---go to mongo application file to get
command prompt
mongochef is browser interface --> if needed can install
//online complier
[Link]
type mongodbcompass in search bar and go to the application - type command in the
mongosh---
database -> collections ->documents(actual data)
cls to clear the screen
or
[Link]()
use database-> to create if not exisitng and connect
use employee -> creates emp database and switches to it
db -> to check current db ur working with
show dbs -> list all databases -> local db is the default one-will not list the
empty db(the one without collections
[Link] () -> drops the current db
[Link](“myTest”)
show collections
[Link]({"Name":"Rama"})-->create collection if does not exist and add
document into it
[Link]()
db.collection_name.insert([comma separated documents])
[Link]({“Name”:”Rama”, ”Name”:”John”,”Name”:”Praveen”})
[Link]()
[Link]().pretty()
//create employee
company db-->employees->documents
[Link]({json1})
[Link]([json1,json2...])
[Link]()->unique object id also displayed
[Link]()
[Link]({"EmpNo":"1"})
[Link]({"Age":{$lt:"30"}})
[Link]({"Age":{$lte:"30"}})
[Link]({"Age"{$gte:"30"}})
[Link]({"Age":{$gt:"30"}})
[Link]({"Age":{$ne:"30"}})
[Link]({"Age":"30"}) //age equal to 30
[Link]( { "birth": { $gt: new Date('1950-01-01') } } )
command to find emps with salary between 10000 and 1000000---in the given db salary
is string
[Link]({"Skill":"MongoDB"})
[Link]({"Skill":"MongoDB","Salary":"80000"})
[Link]({$or:[{"Skill":"MongoDB"},{"Salary":"80000"}]})
[Link]({$and:[{"Skill":"MongoDB"},{"Salary":"80000"}]})
[Link]( { birth: { $gt: new Date('1940-01-01'), $lt: new Date('1960-01-
01') } } )
[Link]({"Skill":"MongoDB",$or:[{"Salary":"80000"},{"Salary":"85000"}]})
[Link]({"Skill":"MongoDB"},{$set:{"Salary":"1000000"}})
[Link]({"Skill":"MongoDB"},{$set:{"Salary":"1000000"}},{multi:true});
//db.collection_name.remove({})
[Link]({"Skill":"MongoDB"},1)
[Link]({"Skill":"MongoDB"})
[Link]({},{"FirstName":1})
[Link]({},{"_id":0,"FirstName":1})
[Link]({},{"_id":0,"FirstName":1,"LastName":1})
[Link]({"Skill":"MongoDB","Salary":"80000"},{"_id":0,"FirstName":1})
[Link]([{$group:{condition of gp}}])
group is similar to group by
grouping cond'n specifed using id followed by the field
total - title
1 indicates true, sum of docs in group
[Link]([{$group:{_id:"$Gender",Total:{"$sum":1}}}]);
[Link]([{$group:{_id:"$Gender",MaxAge:{"$max":"$Age"}}}]);
MaxAge - field title
max operation is performed on age field
[Link]([{$group:{_id:"$Gender",MinAge:{"$min":"$Age"}}}]);
[Link]([{$group:{_id:"$Skill",Total:{"$sum":1}}}]);
//db.collection_name.count()
[Link]()
[Link]([
{
"EmpNo":"1",
"FirstName":"Andrew",
"LastName":"Neil",
"Age":"30",
"Gender":"Male",
"Skill":"MongoDB",
"Phone":"408-1234567",
"Email":"[Link]@[Link]",
"Salary":"80000"
},
{
"EmpNo":"2",
"FirstName":"Brian",
"LastName":"Hall",
"Age":"27",
"Gender":"Male",
"Skill":"Javascript",
"Phone":"408-1298367",
"Email":"[Link]@[Link]",
"Salary":"60000"
},
{
"EmpNo":"3",
"FirstName":"Chris",
"LastName":"White",
"Age":"40",
"Gender":"Male",
"Skill":"Python",
"Phone":"408-4444567",
"Email":"[Link]@[Link]",
"Salary":"100000"
},
{
"EmpNo":"4",
"FirstName":"Debbie",
"LastName":"Long",
"Age":"32",
"Gender":"Female",
"Skill":"Project Management",
"Phone":"408-1299963",
"Email":"[Link]@[Link]",
"Salary":"105000"
},
{
"EmpNo":"5",
"FirstName":"Ethan",
"LastName":"Murphy",
"Age":"45",
"Gender":"Male",
"Skill":"C#",
"Phone":"408-3314567",
"Email":"[Link]@[Link]",
"Salary":"120000"
}])
[Link]( { item: "card", qty: 15 } )
[Link]( { _id: 10, "item" : "peanuts", "qty" : 200 } )
[Link]( [
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps" , qty: 30 }
] );
[Link]([
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" },
"status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" },
"status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" },
"status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" },
"status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" },
"status": "A" }
]);
SELECT * FROM inventory WHERE status = "D"
[Link]({ status: "D" })
SELECT * FROM inventory WHERE status in ("A", "D")
-->
[Link]({ status: { $in: ["A", "D"] } })
SELECT * FROM inventory WHERE status = "A" AND qty < 30
-->
[Link]({ status: "A", qty: { $lt: 30 } })
-->
[Link]({"item":"journal2","qty":23,"size":
{"h":13,"w":21,"uom":"cm"},"status":"A"});
The following example retrieves all documents from the inventory collection where
status equals either "A" or "D":
SELECT * FROM inventory WHERE status in ("A", "D")
-->
[Link]({ status: { $in: [ "A", "D" ] } })
The following example retrieves all documents in the inventory collection where the
status equals "A" and qty is less than ($lt) 30:
--->
[Link]({ status: "A", qty: { $lt: 30 } })
SELECT * FROM inventory WHERE status = "A" AND qty < 30
The following example retrieves all documents in the collection where the status
equals "A" or qty is less than ($lt) 30:
SELECT * FROM inventory WHERE status = "A" OR qty < 30
-->
[Link]({ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] })
In the following example, the compound query document selects all documents in the
collection where the status equals "A" and either qty is less than ($lt) 30 or item
starts with the character p:
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
-->
[Link]({ status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] })
You seem to be combining SQL-like SELECT statements with MongoDB queries, which are
two different query languages. Below are the corrections to the MongoDB queries
based on your SQL examples.
1. SQL: SELECT * FROM inventory WHERE status = "D"
The equivalent MongoDB query in JavaScript:
[Link]({ status: "D" })
2. SQL: SELECT * FROM inventory WHERE status IN ("A", "D")
The equivalent MongoDB query using $in:
[Link]({ status: { $in: ["A", "D"] } })
3. SQL: SELECT * FROM inventory WHERE status = "A" AND qty < 30
The equivalent MongoDB query:
[Link]({ status: "A", qty: { $lt: 30 } })
4. SQL: SELECT * FROM inventory WHERE status = "A" OR qty < 30
The equivalent MongoDB query using $or:
[Link]({ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] })
5. SQL: SELECT * FROM inventory WHERE status = "A" AND (qty < 30 OR item
LIKE "p%")
The equivalent MongoDB query using $or and a regular expression for the LIKE
condition:
[Link]({
status: "A",
$or: [
{ qty: { $lt: 30 } },
{ item: /^p/ }
]
})
Additionally, there seems to be an error in your insertOne command for adding the
document "item":"journal2" to the inventory collection. The correct syntax should
be:
[Link]({
item: "journal2",
qty: 23,
size: { h: 13, w: 21, uom: "cm" },
status: "A"
})
This ensures proper object notation in JavaScript (using {} to wrap the document
fields).