MongoDB + Mongo Express
MongoDB is a powerful, flexible database that makes storing and retrieving data easy for users. It is designed for high-volume, unstructured data to offer a scalable, suitable solution. MongoDB's approach to storing data is document-based, meaning data is stored in a JSON-like format; that's why developers can store complex structures and nested arrays in MongoDB.
Besides this database's many features for building a stable, scalable application, you can easily connect it to Mongo Express with one click, so you can also manage your data through a web-based interface with less technical knowledge.
One-Click Deployment
With Arvancloud Cloud Container ready-made applications, you can set up MongoDB with just one click.
Using MongoDB
After the application is deployed on the Arvancloud Cloud Container, you can access it through this application's console.
To enter the MongoDB shell, you can use the mongo command. After that, you can run the help command to get a guide to working with the mongo terminal.
Logging In to MongoDB
To log in and authenticate in MongoDB, use the following command:
mongo --username root
You're asked to enter the root password. This is the same password you entered when choosing resources in the Arvancloud panel.
Creating a Database
After logging in to MongoDB, you can use this command to see the list of existing databases:
show dbs
The admin and local databases you see in the output are part of the MongoDB cluster.
To create a new database, run the following command:
use firstdb
Enter the name you want instead of firstdb.
If you run show dbs again, you won't see the name of the new database you created, because MongoDB only creates a new database when you insert data into it. For example, you can create a collection in this database and add data to it with the following command:
db.user.insert({name: "Ali Hafezi", age: 25})
Inserting Data
To add one document to a collection, use the db.collection.insertOne() command, and to add several documents to a collection, use db.collection.insertMany(). For example, the following command adds one document to a collection named archive:
db.archive.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
Or the following command adds three documents to a collection named archive:
db.archive.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
Querying the Database
To get data from MongoDB and query it, we use db.collection.find( {} ). For example, consider the following command:
db.archive.find( {} )
This query shows all data in the archive collection and is equivalent to the following SQL statement:
SELECT * FROM archive
We can also set conditions for each query. For example, the query to get all documents whose status value is D looks like this:
db.archive.find( { status: "D" } )
Its SQL equivalent is:
SELECT * FROM archive WHERE status = "D"
For more details on MongoDB queries, you can use its guide.
Connecting to Mongo Express
Mongo Express is a web-based database management interface that gives you a simple platform for interacting with the database. By connecting MongoDB to Mongo Express, you can easily manage your data, query without code, and control your data in real time.
With Arvancloud Cloud Container ready-made applications, you can set up Mongo Express with just one click.
After selecting this application and setting resources, you need to enter a username and password for it, as well as the MongoDB information, such as username, password, and port.
If you didn't change MongoDB's default settings when creating the application, its username is root and its port is 27017.
After 30 seconds, you can access this application through the free Arvancloud domain or your own domain (if you set one) by entering the username and password you set.
You can then see all your data in this user interface.