1. Concept paper
MongoDB and MySQL are representatives of non-relational databases and relational databases, which can quickly build up the cognition of MongoDB through the contrast between them.
MongoDB |
MySQL |
Databases (database) |
Databases (database) |
Collection (Collection) |
Tables (table) |
Documents (document) |
Records (record) |
For relational databases, in general, we can simply understand that: a database management application, you can create multiple databases (databases), each database can manage many tables (tables), the table is stored in the curd can be recorded (records), This level of management can also be mapped to MongoDB.
Unlike MySQL, MongoDB's data has a flexible pattern. The collection itself does not have a regular check of the document structure, and the design of the table constrains the name of the containing field from the beginning.
It is important to note that MongoDB, as a non-relational database, does not mean that we cannot design a relational structure when we use it, and we can also artificially constrain the type of data contained in the document structure, (unlike MySQL, MongoDB's documentation supports arrays and objects, Sub-documents and other complex data structures can also be designed to resemble the ER diagram of the relationship model.
The following is an example of a book classification tree structure data modeling provided by the official website
?
The design idea of the traditional relational database is to treat each node as a record, and each node holds the _id of its parent node.
_id |
Parent |
Mongodb |
Databases |
Dbm |
Databases |
Databases |
Programming |
Languages |
Programming |
Programming |
Books |
Books |
Null |
This relationship can also be used with MongoDB storage
db.categories.insert( { _id: "MongoDB", parent: "Databases" } )db.categories.insert( { _id: "dbm", parent: "Databases" } )db.categories.insert( { _id: "Databases", parent: "Programming" } )db.categories.insert( { _id: "Languages", parent: "Programming" } )db.categories.insert( { _id: "Programming", parent: "Books" } )db.categories.insert( { _id: "Books", parent: null } )
For several other tree-shaped structures, please refer to the modeling methods
Model Tree Structures
Modeling a Tree in a Document Database
2. Quick-Action article
- Download Install client https://docs.mongodb.com/manual/administration/install-community/
Open MongoDB Service
(1) Create a database storage directory
mkdir -p ~/yourdir/db
Ensure that the current user has read and write access to the directory
(2) Open service
mongod --dbpath <path to data directory>
Before this, make sure that the Mongod command path has been added to the system environment variable
Using the MONGO shell to manipulate the database
(1) Connect to the database and open command mode
mongo --host 127.0.0.1:27017
If you use the default parameters, the following arguments can be omitted
(2) A database showing the current operation
db
(3) Switch database
use <database>
MongoDB automatically creates databases and collections when data is stored for the first time in a database
use myNewDatabasedb.myCollection.insertOne( { x: 1 } );
The above insertOne
statement automatically creates the myNewDatabase
database, with the myCollection
collection.
3.MongoDB additions and deletions to check operation
db.collection.deleteOne()db.collection.deleteMany()
db.collection.updateOne()db.collection.updateMany()db.collection.replaceOne()
db.collection.find()
MongoDB Super Concise Introductory tutorial