MongoDB?
- MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.
- MongoDB stores data as a document, and the data structure is
键值对(key=>value)
composed of. - The MongoDB document is similar
JSON 对象
. The field value can include other documents, arrays and document arrays. - MongoDB is a product between relational and non-relational databases. It is the most versatile and most like relational database among non-relational databases.
1. MongoDB terminology
SQL terminology | Explanation | MongoDB terminology | Explanation |
---|---|---|---|
database | database | database | database |
table | table | collection | set |
row | Row | document | Documentation |
column | Field | field | area |
.和$
Have a special meaning, can only be used in a specific environment
2.MongoDB data type
type of data | description |
---|---|
String | String. Data types commonly used to store data. In MongoDB, UTF-8 encoded strings are legal. |
Integer | Integer value. Used to store values. According to the server you use, it can be classified as 32-bit or 64-bit. |
Boolean | Boolean value. Used to store boolean values (true/false). |
Double | Double-precision floating-point value. Used to store floating point values. |
Min/Max keys | Compare a value with the lowest and highest value of the BSON (binary JSON) element. |
Array | Used to store an array or list or multiple values as a key. |
Timestamp | Timestamp. Record the specific time when the document was modified or added. |
Object | Used for embedded documents. |
Null | Used to create null values. |
Symbol | symbol. This data type is basically equivalent to the string type, but the difference is that it is generally used in languages that use special symbol types. |
Date | Date time. Use UNIX time format to store the current date or time. You can specify your own date and time: create a Date object and pass in the year, month, and day information. |
Object ID | Object ID. ID used to create the document |
Binary Data | Binary data. Used to store binary data. |
Code | Type of code. Used to store JavaScript code in the document. |
Regular expression | Regular expression type. Used to store regular expressions. |
2. MongoDB user authentication login
In the cmd environment:
- Set user name and password
use admin
db.create(
{
user:'root',
pwd:'123456',
roles:[{
role:'userAdminAnyDatabase',
db:'admin'
}]
})
- User authentication login
(1)
mongo --port 27017 -u 'root' -p '123456' --authenticationDatabase 'admin'
(2)
mongo --port 27017
use admin
db.auth('root','123456)
3. MongoDB statement
- Create a database:
use 数据库名;
- Delete the database:
db.dropaDtabase()
- Create a collection:
db.createCollection(name,options)
- Delete collection:
db.集合名.drop()
- Insert document:
db.集合名.insertOne/insertMany(document)
db.集合名.save(document)
- Update documentation:
db.集合名.update()
- Query documents:
db.集合名.find({条件})
- Delete document:
db.集合名.remove({条件})
4. MongoDB index
Index: It is essentially a sorted list, which stores the value of the index and the physical address of the data containing this value.
Function: It can speed up data retrieval, sorting, and grouping, and reduce disk I/O
Generally follow: primary/foreign key columns, columns for comparison, columns for range search, columns that are frequently arranged/columns for grouping
Statement:
- Create index:
db.集合名.createIndex({索引名:1/-1})
- View index:
db.集合名.getIndexes()
db.集合名.find({条件}).explain()
- Delete index:
db.集合名.dropIndex({条件})
Types of:
- Compound index
- Hash index
- Multi-key index
- Unique index
- Sparse index
- TTL index
4. MongoDB aggregate query
db.集合名.aggregate([{$group:{_id:'$artist',著作和:{$sum:1}}}])
$sum $svg
$min $max
$push $addToSet
$first $last
Stage operator:
$project
Modify the document structure, rename, add, delete content in the document
// 1:显示 0:不显示
db.books.aggregate([{$project:{_id:0,title:1,price:1}}])
$match
Filter documents
// 类似与find()
db.books.aggregate([{$match:{
'price':{$gte:150}}}])
$group
Group the documents in the collection, which can be used for statistical results
// 1:升序
db.books.aggregate([{$group:{_id:'$artist',数量:{
'$sum':1}}}])
$sort
Sort (ascending)
// 1:升序
db.books.aggregate([{$sort:{
'price':1}}])
$limit
Limit the number of documents returned
db.books.aggregate([{$limit:2}])
-
$skip
Skip the specified number of documents and return the remaining documents -
$unwind
Divide the documents of the array type in the document into multiple pieces, and the media contains one value in the array according to the document
db.classes.aggregate([{$match:{
'classname':'class1'}},{$unwind:'$student'}])