Summary:
No matter what database we learn, we should learn the basic concepts. In mongodb, the basic concepts are documents, collections, databases, and we’ll cover them one by one.
The following table will help you understand some concepts in Mongo easier.
data base
Run database C:\Program Files\MongoDB\Server\4.0\bin> first; mongo.exe
show dbs —— Displays a list of all databasesUse -- run"use"Command to connect to a specified database.DB -- Implementation"db" The command can display the current database object or collection.
Note: the database name should be all lowercase, up to 64 bytes.
Some database names are reserved and can be accessed directly to databases with special functions.
- admin: From the perspective of authority, this is the “root” database. If a user is added to this database, the user automatically inherits all database privileges. Certain server-side commands can only be run from this database, such as listing all databases or closing the server.
- local: This data will never be copied and can be used to store any set of restricted local single servers.
- config: When Mongo is used for sharding settings, the config database is used internally to store information about sharding.
Documents (similar to data rows)
A document is a set of key value (key-value) pairs (that is, BSON).
MongoDB Documents do not need to set the same field, and the same field does not need the same data type, which is very different from relational databases, MongoDB is a very prominent feature.
What we should pay attention to are:
- The key / value pairs in the document are ordered.
- Values in a document can be not only strings in double quotes, but also several other data types (or even entire embedded documents).
- MongoDBDistinguish type and case.
- MongoDBNo duplicate keys can be found in the document.
- The key to a document is a string. Except for a few exceptions, keys can use any UTF-8 character.
Document key naming specification:
- Keys cannot contain \0 (null characters). This character is used to indicate the end of a key.
- .And $has a special meaning that can be used only in specific circumstances.
- The key at the beginning of the underline is “reserved” (not strictly required).
Collection (similar to database table)
A collection is a MongoDB document group, similar to a table in RDBMS.
Collections exist in databases, and collections have no fixed structure, which means that you can insert data of different formats and types into the collection, but usually the data we insert into the collection has some relevance.
When the first document is inserted, the collection is created.
Legal collection name
- The collection name cannot be an empty string “”.
- The collection name can not contain \0 characters (empty characters), which indicates the end of the collection name.
- The collection name cannot start with “system.”, which is a prefix reserved for the system set.
- The set name created by the user can not contain reserved characters. Some drivers do support inclusion in a collection name, because some system-generated collections contain this character. Do not appear in the name unless you want to visit the collection created by this system.
Examples are as follows:
db.col.findOne()
capped collections
Capped collections It’s a fixed size collection.
It has very high performance and queue expiration features (expiration in insertion order). It’s a bit like the RRD concept.
Capped collectionsIt is a high performance automatic maintenance object insertion sequence. It is very suitable for functions similar to log logging.
Unlike standard collections, you have to explicitly create a capped collection, specifying the size of a collection in bytes. The data storage value of collection is allocated in advance.
Note that the specified storage size contains header information of the database.
db.createCollection("mycoll", {capped:true, size:100000})
- In capped collection, you can add new objects.
- Updates can be made, however, objects do not increase storage space. If it increases, the update will fail.
- Database is not allowed to delete. Use drop () method to delete all rows of collection.
- Note: after deleting, you must explicitly recreate the collection.
- In the 32bit machine, the maximum storage of capped collection is 1e9 (1X10).9)A byte.
MongoDB data type
……