In the era of big data, MySQL and other relational databases can't meet the trend of data explosion. As a non-relational data storage mode, NoSQL is mainly applied to distributed data storage System.
"Disclaimer: Most of the content sources in this article are extracted from the Novice tutorial," NoSQL Introduction, "Only to do the study notes reference"
I. A brief introduction of NoSQL
NoSQL (NoSQL = not-only sql), meaning "not just sql." is a general designation of a database management system that differs from a traditional relational database. Data is stored in a way that is a key-value pair.
Second, Cap theorem
In computer science, the cap theorem (Cap theorem), also known as the Brewer's theorem (Brewer's theorem), points out that it is impossible for a distributed computing system to meet the following three points:
- Consistency (consistency) (all nodes have the same data at the same time)
- Availability (availability) (Ensure that each request responds regardless of success or failure)
- Segregation tolerance (Partition tolerance) (loss or failure of any information in the system will not affect the continued operation of the system)
Third, the installation and configuration of MongoDB
Reference:https://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html
Add the environment variable to path (value:%mongodb installation path%/bin).
Open MongoDB Way, enter directly on the command line: Mongo.exe (if the service is not started, first execute: net start MongoDB)
Iv. comparison of MongoDB and MySQL concepts
SQL Terminology/Concepts |
MongoDB Terminology/Concepts |
Explanation/Description |
Database |
Database |
Database |
Table |
Collection |
Database Tables/Collections |
Row |
Document |
Data record lines/documents |
Column |
Field |
Data fields/Fields |
Index |
Index |
Index |
Table joins |
|
Table connection, MongoDB not supported |
Primary key |
Primary key |
Primary key, MongoDB automatically sets the _id field as the primary key |
V. MongoDB command Line
1. Create a new database
Use databasename;//is selected if DatabaseName exists and is created if it does not exist.
Show dbs;//displays all the library names. The newly created library name is not displayed if it is inserted into the data.
Db.databasename.insert ({"Key": "Value"});//Insert a piece of data into the databasename.
2. Delete Database/Collection
(1) Delete database: First select the database name to delete: use DatabaseName; Delete database: Db.dropdatabase ();
(2) Delete collection: Db.collectionname.drop ();
3. Inserting data into the collection
Db.collectionname.insert ({"Name": "Vike"});
4. Update the data in the collection
Db.collectionname.update ({"Name": "Vike"},{$set: {"name": "Someone"}})
Db.collectionname.save ({"Name": "Vike"},{$set: {"name": "Someone"}})
5. Delete collection data
Db.collectionname.remove ({"Name": "Someone"},1)//delete only one piece of data that meets the criteria = Db.collectionname.deleteOne ({"Name": "Someone"})
Db.collectionname.remove ({})//delete all data within the collection = Db.collectionname.deleteMany ({})
6. Query collection data
Db.collectionname.find (). Pretty ()//pretty () enables data to be formatted to display
Query criteria:
MongoDB versus RDBMS Where statement comparison
If you are familiar with regular SQL data, the following table provides a better understanding of MongoDB conditional statement queries:
Operation |
format |
Example |
similar statements in an RDBMS |
Equals |
{<key>:<value> } |
db.col.find({"by":"菜鸟教程"}).pretty() |
where by = ‘菜鸟教程‘ |
Less than |
{<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
Less than or equal to |
{<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
Greater than |
{<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
Greater than or equal to |
{<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
Not equal to |
{<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
MongoDB and condition
The MongoDB find () method can pass in multiple keys (key), each separated by commas, which is the and condition of the regular SQL.
The syntax format is as follows:
>db. Col. Find({key1:value1, key2:value2}). Pretty()
MongoDB OR Condition
The MongoDB OR conditional statement uses the keyword $orin the following syntax format:
>db. Col. Find(
{ $or:[{key1: value1},{key2:value2 }]}). Pretty()
Mongodb$type operator
The $type operator is based on the Bson type to retrieve the matching data type in the collection and returns the result.
The types that can be used in MongoDB are shown in the following table:
type |
Digital |
Notes |
Double |
1 |
|
String |
2 |
|
Object |
3 |
|
Array |
4 |
|
Binary data |
5 |
|
Undefined |
6 |
is obsolete. |
Object ID |
7 |
|
Boolean |
8 |
|
Date |
9 |
|
Null |
10 |
|
Regular Expression |
11 |
|
Javascript |
13 |
|
Symbol |
14 |
|
JavaScript (with scope) |
15 |
|
32-bit integer |
16 |
|
Timestamp |
17 |
|
64-bit integer |
18 |
|
Min Key |
255 |
Query with -1. |
Max Key |
127 |
|
MongoDB Learning Notes (i)