0% found this document useful (0 votes)
5 views

Mongo File

Uploaded by

Rutuja Bhagat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Mongo File

Uploaded by

Rutuja Bhagat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

https://www.tutorialspoint.com/mongodb/mongodb_insert_document.

htm
https://www.youtube.com/playlist?list=PLC3y8-rFHvwh11bWtwm3_qKvo46uDmaal
www.mongodb.org

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
https://onecompiler.com/mongodb/3zs9u9kvw

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
console.clear()
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

db.dropDatabase () -> drops the current db

db.createCollection(“myTest”)
show collections
db.sample.insert({"Name":"Rama"})-->create collection if does not exist and add
document into it
db.sample.drop()

db.collection_name.insert([comma separated documents])


db.sample.insert({“Name”:”Rama”, ”Name”:”John”,”Name”:”Praveen”})

db.sample.find()
db.sample.find().pretty()
//create employee
company db-->employees->documents
db.employee.insert({json1})
db.employee.insert([json1,json2...])
db.employee.find()->unique object id also displayed

db.employee.findOne()
db.employee.find({"EmpNo":"1"})
db.employee.find({"Age":{$lt:"30"}})
db.employee.find({"Age":{$lte:"30"}})
db.employee.find({"Age"{$gte:"30"}})
db.employee.find({"Age":{$gt:"30"}})
db.employee.find({"Age":{$ne:"30"}})
db.employee.find({"Age":"30"}) //age equal to 30

db.employee.find( { "birth": { $gt: new Date('1950-01-01') } } )

command to find emps with salary between 10000 and 1000000---in the given db salary
is string
db.employee.find({"Skill":"MongoDB"})

db.employee.find({"Skill":"MongoDB","Salary":"80000"})
db.employee.find({$or:[{"Skill":"MongoDB"},{"Salary":"80000"}]})
db.employee.find({$and:[{"Skill":"MongoDB"},{"Salary":"80000"}]})

db.employee.find( { birth: { $gt: new Date('1940-01-01'), $lt: new Date('1960-01-


01') } } )
db.employee.find({"Skill":"MongoDB",$or:[{"Salary":"80000"},{"Salary":"85000"}]})

db.employee.update({"Skill":"MongoDB"},{$set:{"Salary":"1000000"}})
db.employee.update({"Skill":"MongoDB"},{$set:{"Salary":"1000000"}},{multi:true});

//db.collection_name.remove({})
db.employee.remove({"Skill":"MongoDB"},1)
db.employee.remove({"Skill":"MongoDB"})

db.employee.find({},{"FirstName":1})
db.employee.find({},{"_id":0,"FirstName":1})
db.employee.find({},{"_id":0,"FirstName":1,"LastName":1})

db.employee.find({"Skill":"MongoDB","Salary":"80000"},{"_id":0,"FirstName":1})

db.employee.aggregate([{$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
db.employee.aggregate([{$group:{_id:"$Gender",Total:{"$sum":1}}}]);
db.employee.aggregate([{$group:{_id:"$Gender",MaxAge:{"$max":"$Age"}}}]);
MaxAge - field title
max operation is performed on age field
db.employee.aggregate([{$group:{_id:"$Gender",MinAge:{"$min":"$Age"}}}]);
db.employee.aggregate([{$group:{_id:"$Skill",Total:{"$sum":1}}}]);

//db.collection_name.count()
db.employee.countDocuments()

db.employee.insert([
{
"EmpNo":"1",
"FirstName":"Andrew",
"LastName":"Neil",
"Age":"30",
"Gender":"Male",
"Skill":"MongoDB",
"Phone":"408-1234567",
"Email":"[email protected]",
"Salary":"80000"
},
{
"EmpNo":"2",
"FirstName":"Brian",
"LastName":"Hall",
"Age":"27",
"Gender":"Male",
"Skill":"Javascript",
"Phone":"408-1298367",
"Email":"[email protected]",
"Salary":"60000"
},
{
"EmpNo":"3",
"FirstName":"Chris",
"LastName":"White",
"Age":"40",
"Gender":"Male",
"Skill":"Python",
"Phone":"408-4444567",
"Email":"[email protected]",
"Salary":"100000"
},
{
"EmpNo":"4",
"FirstName":"Debbie",
"LastName":"Long",
"Age":"32",
"Gender":"Female",
"Skill":"Project Management",
"Phone":"408-1299963",
"Email":"[email protected]",
"Salary":"105000"
},
{
"EmpNo":"5",
"FirstName":"Ethan",
"LastName":"Murphy",
"Age":"45",
"Gender":"Male",
"Skill":"C#",
"Phone":"408-3314567",
"Email":"[email protected]",
"Salary":"120000"
}])

db.products.insertOne( { item: "card", qty: 15 } )


db.products.insertOne( { _id: 10, "item" : "peanuts", "qty" : 200 } )
db.products.insertMany( [
{ item: "card", qty: 15 },
{ item: "envelope", qty: 20 },
{ item: "stamps" , qty: 30 }
] );

db.inventory.insertMany([
{ "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"

db.inventory.find({ status: "D" })

SELECT * FROM inventory WHERE status in ("A", "D")


-->
db.inventory.find({ status: { $in: ["A", "D"] } })

SELECT * FROM inventory WHERE status = "A" AND qty < 30


-->
db.inventory.find({ status: "A", qty: { $lt: 30 } })

-->
db.inventory.insertOne({"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")
-->
db.inventory.find({ 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:
--->
db.inventory.find({ 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

-->
db.inventory.find({ $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%")
-->
db.inventory.find({ 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:

db.inventory.find({ status: "D" })

2. SQL: SELECT * FROM inventory WHERE status IN ("A", "D")


The equivalent MongoDB query using $in:

db.inventory.find({ status: { $in: ["A", "D"] } })

3. SQL: SELECT * FROM inventory WHERE status = "A" AND qty < 30
The equivalent MongoDB query:

db.inventory.find({ status: "A", qty: { $lt: 30 } })

4. SQL: SELECT * FROM inventory WHERE status = "A" OR qty < 30


The equivalent MongoDB query using $or:

db.inventory.find({ $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:

db.inventory.find({
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:

db.inventory.insertOne({
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).

You might also like