This article describes the basic operations and concepts of MongoDB.
Reference Link
NonSQL?
NonSQL (NoSQL) is a generic term for database management systems other than relational databases (SQL databases).NoSQL databases have a different data model and architecture from traditional relational databases.
Mongo DB?
MongoDB is a type of non-relational database management system (NoSQL DBMS).MongoDB stores data in a JSON format called documents. The data model is Schemaless, which makes it easy to add or change fields.
Database
A Database in MongoDB is the top level of data management in MongoDB and groups related collections (equivalent to tables).
Each Database has a name and can contain multiple collections. Databases are the equivalent of relational databases. They also allow for the maintenance and management of independent data for different applications and use cases.
Create a database Create a database named “mydb” by executing the following command.
use mydb |
This command creates a database with the specified name. If the database already exists, it switches to an existing database.
Collections
MongoDB Collections are groupings of documents (data records) in a MongoDB database. Collections are used to logically group related documents together.
Collections are similar to relational database tables, but with some important differences. Specifically, MongoDB collections do not require specific fields or structures to be predefined. Each document can have different fields and structures.
For example, consider the data model of a blog application. There could be multiple collections in the blog database. For example,
- The “users” collection contains user information.
- The “posts” collection could contain blog posts.
The documents in each collection are represented in JSON (or BSON) format. Each document corresponds to a single data record in the collection. Documents have field-value pairs, each field being uniquely identified within the document.
In MongoDB, collections are created dynamically. If you insert data into a collection that does not already exist, MongoDB automatically creates it.
Documents
MongoDB Documents are units of data handled within the MongoDB database. Documents are represented in JSON format and stored within MongoDB.
Documents correspond to Records in relational databases. Each document is treated as a Data Record in a collection, and one of the characteristics of MongoDB is that documents are schema-less. This means that individual documents can have different fields and structures.
A document consists of pairs of fields and their values.
- A field is a combination of a name (key) and a value
- Can have any number of fields
- Field values can have different data types
- String
- numerical value
- Array
- Embedded documentation
- Etc…
For example, here is an example of a user document: this document contains fields such as “name”, “age” and “email”.” The “address” field contains an embedded document and the “interests” field contains an array.
{ “name”: “John Doe”, “age”: 30, “email”: “john@example.com”, “address”: { “street”: “123 Main St”, “city”: “New York”, “country”: “USA” }, “interests”: [“reading”, “music”, “sports”] } |
In MongoDB, documentation is flexible and fields can be added or changed as required. The flexibility to change the data model has the advantage that it is easy to adapt to application development and data evolution.
Operation
We will now go through the simple operation of MongoDB.
mongodb Shell
Start the mongodb shell with the mongo command.
ubuntu@ubuntu:~$ mongo — > |
use db
Create a Database using the use command.
> use testdb switched to db testdb |
show dbs
Use the show dbs command and if it is Empty in Database, the DB will not be displayed.
> show dbs admin 0.000GB config 0.000GB dbStation 0.000GB local 0.000GB myDB 0.000GB tanks 0.000GB |
Create Collections
Create Collections (equivalent to a Table in SQL) using db.createCollection(). The following Example creates a Collection called MyCollections1.
> db.createCollection(“MyCollections1”) { “ok” : 1 } |
Insert Documents
The next step is to insert Documents (equivalent to Record in SQL) into the Collection you have just defined using insertOne().
>db.MyCollections1.insertOne({plc:”Beckhoff”,model:”CX6920″,TwinCATVersion:3}) { “acknowledged” : true, “insertedId” : ObjectId(“64a0ad21cf8d3947804e3616”) } |
>db.MyCollections1.insertOne({plc:”Mitusbishi”,model:”IQR”,GXWORKSVersion:3}) { “acknowledged” : true, “insertedId” : ObjectId(“64a0adc0cf8d3947804e3617”) } |
>db.MyCollections1.insertOne({plc:”Keyence”,model:”KV8000″,KVStudioVersion:”11.6.G”}) { “acknowledged” : true, “insertedId” : ObjectId(“64a0ade1cf8d3947804e3618”) } |
Find
Now use find() to search for data in Collections.
> db.MyCollections1.find() { “_id” : ObjectId(“64a0ad21cf8d3947804e3616”), “plc” : “Beckhoff”, “model” : “CX6920”, “TwinCATVersion” : 3 } { “_id” : ObjectId(“64a0adc0cf8d3947804e3617”), “plc” : “Mitusbishi”, “model” : “IQR”, “GXWORKSVersion” : 3 } { “_id” : ObjectId(“64a0ade1cf8d3947804e3618”), “plc” : “Keyence”, “model” : “KV8000”, “KVStudioVersion” : “11.6.G” } |
Find with Conditions
You can retrieve the Record you want to retrieve by passing it to find() with the Key. In the example below, the Record with plc=Beckhoff is retrieved in Collections.
db.MyCollections1.find({plc:”Beckhoff”}) { “_id” : ObjectId(“64a0ad21cf8d3947804e3616”), “plc” : “Beckhoff”, “model” : “CX6920”, “TwinCATVersion” : 3 } |
Update
If you want to insert new data into a Collections record, use Update(). In the Example below, the Pair of project:Myproject has been added.
db.MyCollections1.updateOne({plc:”Beckhoff”},{$set:{projectname:”Myproject”}}) { “acknowledged” : true, “matchedCount” : 1, “modifiedCount” : 1 } |
Find Again
Check the data one more time.
> db.MyCollections1.find({plc:”Beckhoff”}) { “_id” : ObjectId(“64a0ad21cf8d3947804e3616”), “plc” : “Beckhoff”, “model” : “CX6920”, “TwinCATVersion” : 3, “projectname” : “Myproject” } > db.MyCollections1.deleteOne({plc:”Keyence”}) { “acknowledged” : true, “deletedCount” : 1 } > db.MyCollections1.find() { “_id” : ObjectId(“64a0ad21cf8d3947804e3616”), “plc” : “Beckhoff”, “model” : “CX6920”, “TwinCATVersion” : 3, “projectname” : “Myproject” } { “_id” : ObjectId(“64a0adc0cf8d3947804e3617”), “plc” : “Mitusbishi”, “model” : “IQR”, “GXWORKSVersion” : 3 } |
Exit
Ctrl+C to Exit the mongodb Shell.
> ^C bye |