WT Unit-4 Mongo DB
WT Unit-4 Mongo DB
WT Unit-4 Mongo DB
You can still have multiple groups of data too. In MongoDB, instead of tables
these are called collections.
Insert Documents
There are 2 methods to insert documents into a MongoDB database.
insertOne()
To insert a single document, use the insertOne() method.
Note: When typing in the shell, after opening an object with curly braces "{"
you can press enter to start a new line in the editor without executing the
command. The command will execute when you press enter after closing the
braces.
db.posts.insertOne({
title: "Post Title 1",
body: "Body of post.",
category: "News",
likes: 1,
tags: ["news", "events"],
date: Date()
})
insertMany()
To insert multiple documents at once, use the insertMany() method.
db.posts.insertMany([
{
title: "Post Title 2",
body: "Body of post.",
category: "Event",
likes: 2,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 3",
body: "Body of post.",
category: "Technology",
likes: 3,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 4",
body: "Body of post.",
category: "Event",
likes: 4,
tags: ["news", "events"],
date: Date()
}
])
Find Data
There are 2 methods to find and select data from a MongoDB
collection, find() and findOne().
find()
To select data from a collection in MongoDB, we can use the find() method.
This method accepts a query object. If left empty, all documents will be
returned.
db.posts.find()
findOne()
To select only one document, we can use the findOne() method.
This method accepts a query object. If left empty, it will return the first
document it finds.
db.posts.findOne()
Update Document
To update an existing document we can use
the updateOne() or updateMany() methods.
updateOne()
The updateOne() method will update the first document that is found matching
the provided query.
Let's see what the "like" count for the post with the title of "Post Title 1":
Now let's update the "likes" on this post to 2. To do this, we need to use
the $set operator.
db.posts.updateOne( { title: "Post Title 1" }, { $set: { likes: 2 }
} )
updateMany()
The updateMany() method will update all documents that match the provided
query.
Example
Update likes on all documents by 1. For this we will use the $inc (increment)
operator:
Delete Documents
We can delete documents by using the methods deleteOne() or deleteMany().
These methods accept a query object. The matching documents will be deleted.
deleteOne()
The deleteOne() method will delete the first document that matches the query
provided.
Example
deleteMany()
The deleteMany() method will delete all documents that match the query
provided.
Example
db.posts.deleteMany({ category: "Technology" })
MongoDB Query Operators
There are many query operators that can be used to compare and reference
document fields.
Comparison
The following operators can be used in queries to compare values:
Logical
The following operators can logically compare multiple queries.
Evaluation
The following operators assist in evaluating documents.
You can use $lookup to create a view over two collections and then run queries against
the view. Applications can query the view without having to construct or maintain
complex pipelines.
Example
])
db.orders.insertMany( [
])
This command uses db.createView() to create a new view named sales based on
the orders collection:
db.createView( "sales", "orders", [
$lookup:
from: "inventory",
localField: "prodId",
foreignField: "prodId",
as: "inventoryDocs"
},
$project:
_id: 0,
prodId: 1,
orderId: 1,
numPurchased: 1,
price: "$inventoryDocs.price"
},
{ $unwind: "$price" }
])
In the example:
The $lookup stage uses the prodId field in the orders collection to "join"
documents in the inventory collection that have matching prodId fields.
The $unwind stage converts the price field from an array to a scalar value.
db.student.find().sort({age:1})
Output:
[
{ _id: ObjectId('600f1abb923681e7681ebdcf'), name: 'Bablu',
age: 18 },
{
_id: ObjectId('600f1abb923681e7681ebdce'),
name: 'Akshay',
age: 19
},
{
_id: ObjectId('600f1abb923681e7681ebdd1'),
name: 'Gourav',
age: 20
},
{
_id: ObjectId('600f1abb923681e7681ebdd0'),
name: 'Rakesh',
age: 21
}
]