MongoDB Basic Queries | CRUD Operations in MongoDB | MongoDB Compass

Опубликовано: 12 Февраль 2022
на канале: Senol Atac
11,750
64

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.


Смотрите видео MongoDB Basic Queries | CRUD Operations in MongoDB | MongoDB Compass онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Senol Atac 12 Февраль 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 11,750 раз и оно понравилось 64 людям.