Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB concept parsing


May 17, 2021 MongoDB


Table of contents


MongoDB concept parsing

No matter what database we study, we should learn the basic concepts, the basic concepts in mongodb are documents, collections, databases, and we'll cover them one by one.

The following table will help you understand some of the concepts in Mongo more easily:

SQL terminology/concepts MongoDB terminology/concept Explanation/description
database database Database
table collection Database tables/collections
row document Data record lines/documents
column field Data field/domain
index index Index
table joins Table connection, MongoDB does not support
primary key primary key Primary key, MongoDB automatically sets _id field to primary key

With the following example, we can also get a more intuitive look at some of the concepts in Mongo:

MongoDB concept parsing


Database

Multiple databases can be established in a mongodb.

MongoDB's default database is "db" and is stored in the data directory.

A single instance of MongoDB can hold multiple separate databases, each with its own collection and permissions, and different databases are placed in different files.

The show dbs command displays a list of all the data.

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
> show dbs
local  0.078GB
test   0.078GB
> 

Executing the "db" command can display the current database object or collection.

$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
> db
test
> 

Run the "use" command to connect to a specified database.

> use local
switched to db local
> db
local
> 

In the above instance command, "local" is the database you want to link to.

In the next section we'll cover the use of commands in MongoDB in more detail.

The database is also identified by its first name. The database name can be any UTC-8 string that meets the following criteria.

  • Cannot be an empty string ("").
  • Must not contain '' (space),. , $,/, and ,0 (empty).
  • Should be all in small case.
  • Up to 64 bytes.

Some database names are reserved for direct access to these databases that have a special effect.

  • admin : From a permissions point of view, this is the "root" database. I f a user is added to the database, the user automatically inherits permissions for all databases. Some specific server-side commands can only run from this database, such as listing all databases or shutting down the server.
  • Local: This data is never copied and can be used to store any collection limited to a single local server
  • config : When Mongo is used for shrapned settings, the config database is used internally to hold information about the shrapned.

Document

The document is a key-value pair (i.e. BSON). MongoDB documents don't need to set the same fields, and the same fields don't need the same data type, which is very different from a relationship database and is a prominent feature of MongoDB.

A simple example of a document is:

{"site":"www.w3cschool.cn", "name":"W3Cschool教程"}

The following table lists the terms for RDBMS and MongoDB:

Rdbms Mongodb
Database Database
Form Collection
Yes Document
Column Field
Table union Embed the document
The primary key Primary key (MongoDB provides key for _id)
Database services and clients
Mysqld/Oracle mongod
mysql/sqlplus mongo

It is important to note that:

  1. Key/value pairs in the document are ordered.
  2. The values in a document can be not only strings in double quotes, but also several other data types (even the entire embedded document).
  3. MongoDB is type- and case-sensitive.
  4. MongoDB documents cannot have duplicate keys.
  5. The key of the document is a string. With a few exceptions, keys can use any UTC-8 character.

Document key naming specification:

  • The key cannot contain .0 (empty character). This character is used to represent the end of the key.
  • .and $have special meaning and can only be used in certain environments.
  • The keys at the beginning of the following dash are reserved (not strictly required).

Collection

A collection is a MongoDB document group, similar to a table in RDBMS (Relational Database Management System: Relational Database Management System).

Collections exist in a database, and collections do not have a 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 will have some relevance.

For example, we can insert documents with the following different data structures into a collection:

{"site":"www.baidu.com"}
{"site":"www.google.com","name":"Google"}
{"site":"www.w3cschool.cn","name":"W3Cschool教程","num":5}

When the first document is inserted, the collection is created.

The legitimate collection name

  • The collection name cannot be an empty string "".
  • The collection name cannot contain a character (empty character), which represents the end of the collection name.
  • The collection name cannot be "system." At the beginning, this is the prefix reserved for the system collection.
  • A user-created collection name cannot contain reserved characters. S ome drivers do support inclusion in collection names because some system-generated collections contain this character. D on't put $in your name unless you want to access collections created by this system.

Here's an example:

db.col.findOne()

capped collections

Capped Collections is a fixed-sizecollection.

It has high performance and queue expiration characteristics (expiration in the order in which it is inserted). It's a bit like the concept of "RRD".

Capped Collections is a high-performance, automated order in which objects are inserted. I t's great for logging-like features and different from standardcollection, where you have to explicitly create a capped collection that specifies the size of a collect in bytes. The data storage space value of the collection is allocated in advance.

Note that the specified storage size contains header information for the database.

db.createCollection("mycoll", {capped:true, size:100000})
  • In the collection, you can add new objects.
  • can be updated, however, the object does not increase storage space. If you increase, the update fails.
  • The database is not allowed to be deleted. Use the drop() method to delete all lines of the action.
  • Note: After deletion, you must explicitly re-create this action.
  • In 32bit machines, capped collections have a maximum storage of 1e9 (1X10 9) bytes.

Metadata

The information for the database is stored in the collection. They use the system's namespace:

dbname.system.*

In the MongoDB database, the namespace , the namespace, the collection, which contains a variety of system information, is as follows:

The collection namespace Describe
dbname.system.namespaces List all namespaces.
dbname.system.indexes All indexes are listed.
dbname.system.profile Contains database profile information.
dbname.system.users List all users who have access to the database.
dbname.local.sources Contains server information and status for replication of the slave.

There are the following restrictions for modifying objects in the system collection.

You can create an index by inserting the data in the . system.indexes. But beyond that, the table information is immeascondable (the special drop index command automatically updates the information).

It's modifiable. It's removable.


MongoDB data type

The following table shows several data types commonly used in MongoDB.

The data type Describe
String String. T he type of data commonly used to store data. In MongoDB, UTF-8 encoded strings are legal.
Integer Integer values. U sed to store values. Depending on the server you use, it can be divided into 32-bit or 64-bit.
Boolean Boolean value. Used to store Boolean values (true/false).
Double Double floating-point value. Used to store floating-point values.
Min/Max keys The ratio of a value to the lowest and highest values of the BSON (binary JSON) element.
Arrays Used to store an array or list or multiple values as a key.
Timestamp Time stamp. Record the exact time the document was modified or added.
Object Used for inline documents.
Null Used to create an empty value.
Symbol Symbol. This data type is basically equivalent to a string type, but the difference is that it is typically used in languages with special symbol types.
Date The date time. U se the UNIX time format to store the current date or time. You can specify your own date time: create a Date object and pass in year-to-month information.
Object ID The object ID. The ID used to create the document.
Binary Data Binary data. Used to store binary data.
Code The type of code. Used to store JavaScript code in documents.
Regular expression The regular expression type. Used to store regular expressions.