Hi guys,
In this lesson, we talk about basic CRUD queries on mongoDB. Like RDBMS, CRUD operations can be handled on MongoDB easily.
To show them we will go on with examples.
Our first operation will be to create a Database. By the way, we can reach to mongoDB databases from mongo shell or MongoDB Compass.
To create a database our query is “use db_name”. For example, if we want to create a database like db_test, our query will be “use db_test“.
Then the database consists of collections. To create a collection, our command will be “db.createCollection(“collection_name”)” for example: db_collection
These operations also can be handled via MongoDB Compass User Interface.
Then the collection consists of documents. To create document, our command will be “db.collectionName.insertOne(and data)”
In here data format will be json. For example; name, role etc.
Then to select item from collections, our command will be db.collectionName.find(and where condition); Where condition also should be in json format.
On MongoDB, we can also aggregate data. Like group by, sum etc. Actually, in MongoDB, to do it, we have aggregate function of collection.
To aggregate it, our command will be db.collectionName.aggregate(aggregateType, aggregateKey and aggregateResult); For example, group by users via role then count them according to role name.
db.db_collection.aggregate([
{"$group" : {_id:"$role", count:{$sum:1}}}
])
Ok, now let’s check MongoDB Compass User Interface. Actually, this all commands can be handled on MongoDB Compass User Interface.
On MongoDB Compass User Interface, we can filter collection records with conditions, projections and sortings. These all formats should be in json format.
On MongoDB Compass User Interface, we can aggregate collection data. Actually in here, you will notice, there are some stages. This means mongoDB provides aggregation as pipeline. So first of all, you can group data then you can sort it.
On MongoDB Compass User Interface, we can analyze the schema structure of collection data.
On MongoDB Compass User Interface, we can analyze queries. You know that we can generally use it to understand the performance of queries as cost and time.
On MongoDB Compass User Interface, we can create indexes for performance. We will see an example about it in the next lesson.
On MongoDB Compass User Interface, we can describe rules like RDBMS. For example, we can describe not null fields, or we can describe field types.
db.runCommand({
collMod: "users",
validator: {
required: ["user-id"],
properties: {
"user-id": {
bsonType: "int",
description: "must be an integer"
},
}
}})
That’s all.
Thank you.
Watch video MongoDB Basic Queries | CRUD Operations in MongoDB | MongoDB Compass online without registration, duration hours minute second in high quality. This video was added by user Senol Atac 12 February 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 11,750 once and liked it 64 people.