MongoDB Compass is a visual GUI tool that allows us to operate MongoDB through a visual interface.
Installing MongoDB Compass
For Windows system, refer to the Windows environment installation of MongoDB section. For MacOS system, refer to the MacOS environment installation of MongoDB section.
Connecting to MongoDB Server
Open Compass
If you haven't set up an account and password when installing MongoDB locally, just click "Connect" to use the default connection to log in to the local MongoDB.
If an account and password have been set up, or if you want to connect to a remote MongoDB, click on the top right corner to enter the connection configuration page.
Enter MongoDB connection information
Main Interface
Creating a Database
Based on the above image, click to enter the database creation window
Tip: When creating a database in Compass, you must also create a collection, hence the input of a collection name in the above image.
Collection Operations
Inserting Data
Enter the document JSON data and click "Insert" to insert the data. If entering a JSON array, it represents inserting multiple pieces of data. The following are the documents being inserted:
[{
"item": "journal",
"qty": 25,
"size": {
"h": 14,
"w": 21,
"uom": "cm"
},
"status": "A"
}, {
"item": "notebook",
"qty": 50,
"size": {
"h": 8.5,
"w": 11,
"uom": "in"
},
"status": "A"
}, {
"item": "paper",
"qty": 100,
"size": {
"h": 8.5,
"w": 11,
"uom": "in"
},
"status": "D"
}, {
"item": "planner",
"qty": 75,
"size": {
"h": 22.85,
"w": 30,
"uom": "cm"
},
"status": "D"
}, {
"item": "postcard",
"qty": 45,
"size": {
"h": 10,
"w": 15.25,
"uom": "cm"
},
"status": "A"
}]
Querying Data
Modifying & Deleting Data
Creating Index
Switch to the collection index panel
Create Index
Analyzing Query Performance
Similar to MySQL's explain
, MongoDB also supports the explain
statement to analyze the performance of query statements.