Let's start by introducing the basic concepts of MongoDB:
The left column is the term for the relational database, and the right column is the term NoSQL and MongoDB.
Database:database Database
Table:collection Database Tables/Collections
Row:document data record lines/documents
Coloum:field data fields/fields
Index:index Index
Table joins: Tables connection, MongoDB not supported
Primary Key:primay Key Primary key, MongoDB automatically sets the ID as the primary key
First look at the concept of the database, after entering the MONGO with show DBS can see all the current data list: Just install the admin and local two database
> Show DBS
Admin 0.000GB
Local 0.000GB
Use command to select the database that needs to be used.
> Use admin
Switched to DB admin
If we want to create a database of our own, we can also use the using command. You can see that the newly created database is not visible when you use show DBS. This requires us to insert the data
> Use Maple
Switched to DB Maple
> Show DBS
Admin 0.000GB
Local 0.000GB
> DB
Maple
You need to introduce the concepts of documents and collections before inserting data:
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 require the same data type, which is very different from the relational database, is also a very prominent MongoDB features. An example of a document: {' name ': ' ZHF '}. This is the same as the format of the dictionary.
The document format needs to be noted:
- The key / value pairs in the document are ordered.
- The values in the document can be not only strings in double quotes, but also several other data types (even the entire embedded document ).
- MongoDB distinguishes between type and case.
- MongoDB documents cannot have duplicate keys.
- The key of the document is a string. In addition to a few exceptions, keys can use any UTF-8 character.
Document key naming specification:
- The key cannot contain a /s ( null character ). This character is used to denote the end of a key.
- . and the $ there is a special meaning that can only be used in certain circumstances.
- Keys that begin with the underscore "_" are reserved ( not strictly required ).
Collection:
The collection is the MongoDB document Group, which is the table that corresponds to the relational database when we introduce the term. The document corresponds to the row of the relational database. So a collection can be seen as a collection of documents. The collection exists in the database, the collection does not have a fixed structure, which means that you can insert data in different formats and types on the collection, but usually we have some relevance to the data that we insert into the collection. For example, we can insert a document of the following different data structures into the collection:
{' name ': ' ZHF '}
{' name ': ' ZHF ', ' City ': ' Chengdu '}
{' name ': ' ZHF ', ' City ': ' Chengdu ', ' Age ': 30}
From this we can see that the data structure ofMongoDB is more arbitrary than the data in the relational database. The data type of each row is not required to be the same.
Command specification for Collections
- The collection name cannot be an empty string "".
- The collection name cannot contain the \ s character (null character ), which represents the end of the collection name.
- The collection name cannot be "system." begins with a prefix reserved for the system collection.
- User-created collection names cannot contain reserved characters. Some drivers do support inclusion in the collection name, because some system-generated collections contain that character. Never show $ in a name unless you want to access a collection created by this system .
After we've introduced the documents and collections, we're going to insert a set of data into the database. After inserting, we can find our database by show DBS .
> Db.maple.insert ({"Name": "ZHF"})
Writeresult ({"ninserted": 1})
> Show DBS
Admin 0.000GB
Local 0.000GB
Maple 0.000GB
The preceding db.maple.insert () command actually inserts a document into the Maple collection in the maple database { "name": "ZHF"}
Through db. Collection Name . Find () the contents of the collection can be queried in the same way
> Db.maple.find ()
{"_id": ObjectId ("5a3133be956c5a2e19343140"), "name": "ZHF"}
You can list all the collections that exist by using show collections.
> Show Collections
Maple
If you want to delete the collection, use db. Collection Name . Drop () the way
> Db.maple.drop ()
True
> Show Collections
If you want to delete the database. Use the db.dropdatabase () command
> Db.dropdatabase ()
{"Dropped": "Maple", "OK": 1}
> Show DBS
Admin 0.000GB
Local 0.000GB
MongoDB Learning: Database