mongodb
-
The default port number of MYSQL: 3306
-
The default port number of Mongodb: 27017
NOSQL non-relational database management system
Used for ultra-large-scale data storage (eg: Google, Facebook) does not require a fixed model, and can be scaled horizontally without unnecessary operations
What is MongoDB?
Written in C++, it is an open source database system based on distributed file storage.
In the case of high load, adding more nodes can ensure server performance.
- Provide scalable high-performance data storage solutions for WEB applications
- MongoDB data is stored as a document, and the data structure is composed of key-value (key=>value) pairs
- MongoDB documents are similar to JSON objects, and the field values can include other documents, arrays and document arrays
Concept analysis
SQL | MongoDB | explain |
---|---|---|
table | collection | set |
row (one row, one record) | document | Documentation |
column | field | Domain (key-value pair) |
index | index | index |
table joins | Table connection query is not supported | |
primary key | primary key | Primary key, mongoDB automatically sets the _id field as the primary key |
database
db. Different databases are placed in different files
show dbs can display a list of all databases
data storage name:
Cannot be a character string (""), no spaces, .,,/,\0 (empty characters) It is best not to use Chinese (Chinese garbled characters may occur), all lowercase, up to 64 bytes
Documentation:
Key-value pair, BSON
{”site":"xxxxx","age":25}
Database service and client | ||
---|---|---|
MYSQL/Oracle | mongod | |
note:
- The key-value pairs in the document are ordered
- Type and case sensitive
- There can be no duplicate keys,
- Key: string (utf-8), cannot contain empty characters
- . $ Has a special meaning, only used in a specific environment
- The key at the beginning of _ is reserved (not strictly required)
object
onject_id can be of any type, generally the default is object type
The created timestamp is saved in onject_id, so there is no need to create timestamp by yourself (timestap)
[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-IpPiTmmw-1611392732234)(C:\Users\jry\AppData\Roaming\Typora\typora-user-images\ image-20210123134431496.png)]
Get the creation time of the document through the getTimestap function
Convert to string form by str
date
[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-iqoE0EMq-1611392732240)(C:\Users\jry\AppData\Roaming\Typora\typora-user-images\ image-20210123134725981.png)]
[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-eBUxv13Z-1611392732243)(C:\Users\jry\AppData\Roaming\Typora\typora-user-images\ image-20210123134808410.png)]
Basic operations of MongoDB
One, create a database
use xxx
But there is no content in the database, it will be displayed after inserting the content
Two, delete the database
db.dropDatabase()
Switch to another database to see that it has been deleted
Three, create a collection
- db.createCollection("employee") creates a collection of employee
- db.mycol(collection name).insert({"_id":01,"name":"马云"}) Insert directly, there is no collection will automatically create a mycol collection
- show collections show the collections under the database
- db view the current database
Fourth, delete the collection
db.tc (collection name).drop()
Five, insert documents into the collection
db.collection name.insert(document) If _id exists, the primary key exception will be thrown and the current data will not be saved
db.collection name.save(document) If _id exists, update the data, if it does not exist, insert the data
Insert a record
var document name = db.collection name.insert({insert key-value pair content})
Insert multiple records
var document name = db.collection name.insertOne({})
Insert multiple documents
var document name = db.collection name.insertMany({})
Insert array
1. Create an array first
Sixth, the update of the document
db.collection name.updata(
{query—query condition}
upsert–true: insert if it does not exist, update if it exists, false: do not update
{$set"{updated value}}—why update value
multi:true/false—true: update all qualified values, false: update only the first qualified value
)
Update a value
db.employee.update({“sex”:“男”},{$set:{“age”:18}})
All updates that meet the conditions
db.employee.update({“sex”:“男”},{$set:{“age”:18}},{multi:true})
Seven, document query
1. Condition mark
$gt | > |
---|---|
$gte | >= |
$lt | < |
$ lte | <= |
$ do | != |
$eq | 、= |
Query records whose age is less than 19
Query records whose age is less than or equal to 19
Check if the age is less than 30 and the address is in Yan'an
db.employee.find({“age”:{$lt:20},“address”:“延安”})
2. AND OR condition
AND
db.employee.find({'KaTeX parse error: Expected '}', got 'EOF' at end of input: and':[{"age":{ lt:20}},{“address”:“延安”}]})
'$and'-and relational character
[{Condition 1}, {Condition 2}]
OR
db.employee.find({'KaTeX parse error: Expected '}', got 'EOF' at end of input: or':[{"age":{
lt:20}},{“address”:“延安”}]}))
db.employee.find({"KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"女"},{"age":{
gt:10}}]})
Gender is female, age>10
Query on robo 3T
AND and OR combined
First and then or
db.employee.find({“sal”:{ KaTeX parse error: Expected 'EOF', got '}' at position 9: gt:17000}̲,'or’:[{“sex”:“女”},{“address”:“延安”}]})
db.employee.find({“sal”:{ KaTeX parse error: Expected 'EOF', got '}' at position 9: gt:19000}̲,"or":[{“sex”:“女”},{“address”:“延安”}]})
db.getCollection('employee').find({"age":{$lt:30},"address":"郑州"})db.employee.find({"age":{$lt:30},"address":"郑州"})
//查询age小于30并且地址是郑州的document
db.employee.find({'$and':[{"age":{$lt:30}},{"address":"郑州"}]}) db.getCollection('employee').find({})
//查询age小于30或address是洛阳document
db.employee.find({'$or':[{"age":{$lt:30}},{"address":"洛阳"}]})
//查询sex为女或address是南京的document
db.employee.find({'$or':[{"sex":"女"},{"address":"南京"}]})
// (price>70 and (publish="机械工业出版社" or public="新疆儿童出版社"))
db.db_book.find({"price":{$gt:70},'$or':[{"publish":"机械工业出版社"},{"publish":"新疆儿童出版社"}]})
8. Fuzzy query
- /content/
- /^ What starts with/
- /Ends with $/
Those with Jiang in their name
db.employee.find({“name”:/蒋/})
start with the word horse in name
db.employee.find({“name”:/^马/})
The name that ends with the word according to
db.employee.find({“name”:/依$/})
Nine, delete documents
db.collection name.remove({specify conditions})
db.employee.remove({_id:1002}) 删除_id为1002的文档
db.collection name.remove (delete condition, 1)
db.employee.remove({name:/诸/},1) 删除名字中有诸的,并且只删除一个文档
The name that ends with the word according to
db.employee.find({“name”:/依$/})