Session 15
Session 15
o Create Operations
Create a database in MongoDB
Create a new database run any command against a non-existing database, and MongoDB
will automatically create it for you.
Use: insertOne() or insertMany() methods. If database is not available then first it will
create the database.
insertOne(): The insertOne() method is used to insert a new document into a collection.
[Note: cls command will be used for clear the screen.]
To show whether data base is created or not use
Show dbs command.
To switch between the database, use the command:
Use database_name;
Creating a New Collection:
Syntax: db.createCollection(name)
Example: db.createCollection("users")
Topic2.Creating Records: Create new documents to a collection in MongoDB:
B.[Read Operations]
1. Read: The process of retrieving or viewing data from your database. In MongoDB,
this is done using the find() and findOne() methods.
Syntax: db.collectionName.find(query, projection)
Query - It specifies the selection criteria for the documents to be retrieved.
Projection - It specifies which fields to include or exclude in the result set.
Note: Both query and projection are optional.
to include (1) or exclude (0) the field in the result set.
A. Find() : db.students.find({"regNo":"3014"})
Example: db.users.find()
Example: db.users.find({ age: { $gt: 29 } }, { name: 1, age: 1 })
B. findOne():The findOne() method is used to retrieve a single document from a
collection.
Syntax: db.collectionName.findOne()
let student = db.collection('students').findOne({ name: 'Logged In Student' });
console.log(student);
Example: db.users.findOne({ name: "Jim" })
o Update Operations:
Update" operation is used to modify existing documents in a collection. We have
three updateOne(), updateMany(), or replaceOne() methods.
A. updateOne()
The updateOne() method is used to update a single document that matches a
specified filter.
Syntax: db.collectionName.updateOne(filter, update, options)
a.Update: an optional Boolean that specifies whether to insert a new
document if no document matches the filter.
If Update is set to true and no document matches the filter, a
new document will be inserted. The default value of Update is
false.
Example: db.users.updateOne({ name: "Angela" },
{ $set: { email: "[email protected]" } })
Available operations:
$set: Sets the value of a field in a document. If the field does not exist,
the set will create it.
$unset: Removes a field from a document.
$inc: Increments the value of a field in a document by a specified amount.
db.products.insertOne(
_id: 1,
Product: "Car",
quantity: 10,
{ Product: "Car" },
db.products.find() // output
{
_id: 1,
Product: 'Car',
quantity: 7,
metrics: {
orders: 4,
ratings: 4.5
}
}
$push: Adds an element to the end of an array field in a document. If the field does not
exist, push will create it as an array with the specified element.
db.sunil2.updateOne({ name: "Angela" },
{ $push : { "Gender":"Male" } })
$pull: Removes all occurrences of a specified value from an array field in a document.
db.fruit.insertMany( [
{
_id: 1,
fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
vegetables: [ "carrots", "celery", "squash", "carrots" ]
},
{
_id: 2,
fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ],
vegetables: [ "broccoli", "zucchini", "carrots", "onions" ]
}
])
db.fruit.find()