Nosql and relational databases

1. What is noSQL?

2, the difference between a relational database and a relational database:

3. Non-relational status in website architecture

Install and configure MongoDB

Introduction to MongoDB: MongoDB is written by C++ language, is an open source database system based on distributed file storage, in the case of high load, add more nodes, can ensure server performance, MongoDB aims to provide scalable high-performance data storage solutions for WEB applications

MongoDB stores data as a document, and the data structure is composed of key=>value pairs. MongoDB documents are similar to JSON objects, and the field values can contain other documents, arrays and document arrays

1. Select custom installation

2. Remember the installation directory

Configuration:1. Create a datas folder at the same level as the bin directory

2, Create the data storage data folder and logs log folder under datAS

3. Configure environment variables

Place the bin directory in the configuration environment variable

4. Configure services

The service starts in the background on Windows as the Linux equivalent

Install commands:

Mongod --dbpath C:\MongoDB\Server\3.4\datas\data --port 27017 --logpath C:\MongoDB\Server\3.4\datas\logs\mongo.log --logappend --serviceName "MongoDB" --installCopy the code

Explanation:

Mongod --dbpath C:\MongoDB\Server\3.4\datas\data # Install path --port 27017 # port number --logpath C:\MongoDB\Server\3.4\datas\logs\mongo.log # location of the log file --logappend --serviceName "MongoDB" # serviceName --installCopy the code

Note: CMD must be opened in administrator mode

MongoDB client and server commands

The purpose of setting environment variables is to find the exe tool, which is used in CMD or Python to use external programs, in these directories of environment variables

For databases, there are two commands

Server command: Start the database

The default port for Mongo is 27017

mongod

— port-p Which port is on —

— DBPath Specifies the location where data is stored after the database is started

Client commands: Connect and start the database for basic data operations

mongo

Port – the port

​ –host ip

Basic terms and commands of MongoDB

SQL terms/concepts MongoDB terminology/concepts Explanation/explanation
database database The database
table collection Database tables/collections
row document Data record line/document
column field Data fields/fields
index index The index
table joins Table joins, MongoDB does not support
primary key primary key Primary key. MongoDB automatically sets the _ID field as the primary key

2. Collection

Collections are groups of MongoDB documents, similar to tables in a relational database

Collections have no fixed structure, which means you can insert different formats and types of data into collections

2, document –document

A document is a set of key-value pairs (BSON) –document is a js pair, {}

Json -Bson is the binary format of JSON, —– JS —– of Mongo’s underlying execution engine is the JS engine, so you can run JS code in the Mongo client

The value in the document can be not only the string inside the double quotes, but also several other data types (or even the entire embedded document) : {key:value}–{key:value(with corresponding data)}

5. MongoDB data insertion and search

Show DBS — Shows which databases are available

Use datebase_name– select database — create database implicitly

Show Collections /tables — View all collections under a database

1. Create data

By default, when you enter Mongo, there is a test database

Step 1: Use a nonexistent library

Step 2: Store the content in the database to create it

2. Create a collection – the data will be created successfully

Mongo collections can also be created implicitly: you don’t have to pre-create collections or databases

Database – Switching data to use- creates a database by inserting a piece of data or creating a collection

Collection:

Db.set name.insert ()– Sets are also created automatically for us

MongoDB uses the insert() method to insert documents into the collection with the following syntax:

1. Insert a single strip

Db.collection_name. Insert ({single document})

2. Insert multiple inserts

​ db.col_name.insert(

[{single document}, {single document}, {single document},….]

Example:

[{db. Shop. Insert (" goods_id ": 1," cat_id ": 4," goods_name ":" KD876 ", "goods_number" : 1, "click_count" : 7, "shop_price" : 1388.00, "add _time":1240902890},{"goods_id":4,"cat_id":8,"goods_name":"\u8bfa\u57fa\u4e9aN85\u539f\u88c5\u5145\u7535\u5668","goods_nu Mber: 17, "" click_count" : 0, "shop_price: 58.00, add_time" : 1241422402}, {" goods_id ": 3," cat_id ": 8," goods_name ":" the \ u8bfa \ u57fa \ u 4 e9a \ u539f \ u88c55800 \ u8033 \ u673a ", "goods_number click_count" : 24, "" : 3," shop_price ": 68.00," add_time ": 1241422082}, {" goods_id ":5,"cat_id":11,"goods_name":"\u7d22\u7231\u539f\u88c5M2\u5361\u8bfb\u5361\u5668","goods_number":8,"click_count":3,"shop _price: 20.00, add_time ": 1241422518}, {" goods_id" : 6, "cat_id" : 11, "goods_name" : "\ u80dc \ u521bKINGMAX \ u5185 \ u5b58 \ u5361", "goo Ds_number: 15, "" click_count" : 0, "shop_price: 42.00, add_time" : 1241422573}, {" goods_id ": 7," cat_id ": 8," goods_name ":" \ u8bfa \ u5 7 fa \ u4e9aN85 \ u539f \ u88c5 \ u7acb \ u4f53 \ u58f0 \ u8033 \ u673aHS - 82 ", "goods_number" : 20, "click_count" : 0, "shop_price" : 100.00, "add_ time":1241422785},{"goods_id":8,"cat_id":3,"goods_name":"\u98de\u5229\u6d669@9v","goods_number":1,"click_count":9,"shop_ Price: 399.00, add_time ": 1241425512}, {" goods_id" : 9, "cat_id" : 3, "goods_name" : "\ u8bfa \ u57fa \ u4e9aE66", "goods_number" : 4, "cli Ck_count: 20, "" shop_price" : 2298.00, "add_time" : 1241511871}, {" goods_id ": 10," cat_id ": 3," goods_name ":" \ u7d22 \ u7231C702c ", "goo Ds_number: 7, "" click_count" : 11, "shop_price" : 1328.00, add_time: 1241965622}, {" goods_id ": 11," cat_id ": 3," goods_name ":" \ u7d22 \ u7231C702c goods_number ", "" : 1," click_count ": 0," shop_price: 1300.00, add_time ": 1241966951}, {" goods_id" : 12, "cat_id" : 3, "goo Ds_name ":" \ u6469 \ u6258 \ u7f57 \ u62c9A810 ", "goods_number" : 8, "click_count" : 13, "shop_price" : 983.00, "add_time" : 1245297652}]) db.shop.insert([{"goods_id":13,"cat_id":3,"goods_name":"\u8bfa\u57fa\u4e9a5320 XpressMusic goods_number ", "" : 8," click_count ": 13," shop_price: 1311.00, add_time ": 1241967762}, {" goods_id" : 14, "cat_id" : 4, "go Ods_name \ u8bfa \ u57fa \ u4e9a5800XM ":" ", "goods_number click_count" : 1, "" : 6," shop_price: 2625.00, add_time ": 1241968492}, {" good S_id: 15, "" cat_id" : 3, "goods_name" : "\ u6469 \ u6258 \ u7f57 \ u62c9A810", "goods_number click_count" : 3, "" : 8," shop_price ": 788.00," the AD d_time":1241968703},{"goods_id":16,"cat_id":2,"goods_name":"\u6052\u57fa\u4f1f\u4e1aG101","goods_number":0,"click_count" : 3, "shop_price" : 823.33, "add_time" : 1241968949}, {" goods_id ": 17," cat_id ": 3," goods_name ":" \ u590f \ u65b0N7 ", "goods_number" : 1," Click_count ": 2," shop_price ": 2300.00," add_time ": 1241969394}, {" goods_id" : 18, "cat_id" : 4, "goods_name" : "\ u590f \ u65b0T5", "good S_number click_count ": 1," ": 0," shop_price ": 2878.00," add_time ": 1241969533}, {" goods_id" : 19, "cat_id" : 3, "goods_name" : "the \ u4e09 \ u 661 FSGH - F258 goods_number ", "" : 12," click_count ": 7," shop_price: 858.00, add_time ": 1241970139}, {" goods_id" : 20, "cat_id" : 3, "go Ods_name \ u4e09 \ u661fBC01 ":" ", "goods_number click_count" : 12, "" : 14," shop_price ": 280.00," add_time ": 1241970417}, {" goods_id" : 2 1,"cat_id":3,"goods_name":"\u91d1\u7acb A30, "" goods_number: 40," "click_count" : 4, "shop_price: 2000.00, add_time" : 1241970634}, {" goods_id ": 22," cat_id ": 3," goods_name ":" \ u591a \ u666e \ u8fbeTouch HD ", "goods_number click_count" : 1, "" : 15," shop_price ": 5999.00," add_time ": 1241971076}]) db.shop.insert([{"goods_id":23,"cat_id":5,"goods_name":"\u8bfa\u57fa\u4e9aN96","goods_number":8,"click_count":17,"shop_p Rice: 3700.00, add_time ": 1241971488}, {" goods_id" : 24, "cat_id" : 3, "goods_name" : "P806", "goods_number:" 100, "click_count" : 35," Shop_price ": 2000.00," add_time ": 1241971981}, {" goods_id" : 25, "cat_id" : 13, "goods_name" : "\ u5c0f \ u7075 \ u901a \ / \ u56fa \ u8bdd50 \ u 5143 \ u5145 \ u503c \ u5361 ", "goods_number click_count" : 2, "" : 0," shop_price ": 48.00," add_time ": 1241972709}, {" goods_id" : 26, "cat_i d":13,"goods_name":"\u5c0f\u7075\u901a\/\u56fa\u8bdd20\u5143\u5145\u503c\u5361","goods_number":2,"click_count":0,"shop_p Rice: 19.00, add_time ": 1241972789}, {" goods_id" : 27, "cat_id" : 15, "goods_name" : "\ u8054 \ u901a100 \ u5143 \ u5145 \ u503c \ u5361", "go Ods_number click_count ": 2," ": 0," shop_price ": 95.00," add_time ": 1241972894}, {" goods_id" : 28, "cat_id" : 15, "goods_name" : "\ u8054 \ U901a50 \ u5143 \ u5145 \ u503c \ u5361 ", "goods_number" : 0, "click_count" : 0, "shop_price" : 45.00, "add_time" : 1241972976}, {" goods_id ": 14, 29, "cat_id" : "goods_name" : "\ u79fb \ u52a8100 \ u5143 \ u5145 \ u503c \ u5361", "goods_number" : 0, "click_count" : 0, "shop_price" : 90.0 0,"add_time":1241973022},{"goods_id":30,"cat_id":14,"goods_name":"\u79fb\u52a820\u5143\u5145\u503c\u5361","goods_number" : 1, 9, "click_count" : "shop_price" : 18.00, "add_time" : 1241973114}, {" goods_id ": 31," cat_id ": 3," goods_name ":" \ u6469 \ u6258 \ u7f57 \ u62c9E8 ", "goods_number" : 1, "click_count" : 5, "shop_price" : 1337.00, add_time: 1242110412}, {" goods_id ": 32," cat_id ": 3," goods_name ":" \ U8bfa \ u57fa \ u4e9aN85 goods_number ", "" : 4," click_count ": 9," shop_price ": 3010.00," add_time ": 1242110760}])Copy the code

4. MongoDB deletes the database

Grammar: db.dropdatabase ()

Delete the current database, default is test, you can use the db command to view the current database name

5. MongoDB creates and deletes collections

MongoDB create collection:

Db.createcollection (Name,options)

Parameter Description:

  • Name: Name of the collection to be created
  • Options: Optional parameters that specify memory size and index options

Options can be the following parameters:

field type describe
capped Boolean (Optional) If true, a fixed collection is created. A fixed collection is a collection with a fixed size, and when it reaches its maximum, it automatically overwrites the earliest documents. When this value is true, the size argument must be specified
autoIndexId Boolean (Optional) If the value is true, the index is automatically created in the _ID field. The default is true
size The numerical (Optional) Specify a maximum value (in bytes) for the fixed collection. This field also needs to be specified if capped is true
max The numerical (Optional) Specifies the maximum number of documents to contain in a fixed collection

6. Delete the collection

Grammar: db. Collection. Drop ()

Query expressions in MongoDB

Query expressions in MongoDB are equivalent to query conditions in SQL where clauses and can be used to filter data. When we delete, update or search, we need query expression as the first condition to filter which data to delete 131132 or update or search, so a good command of query expression is the key to master THE CRUD in MongoDB

instructions grammar describe
Is equal to the {field:value} Field = value
Less than {field:{$lt:value}} Field < value
Is greater than {field:{$gt:value}} Field > value
Less than or equal to {field:{$lte:value}} Field <= value
Greater than or equal to {field:{$gte:value}} Field >= value
Is not equal to {field:{$ne:value}} Field! = value
$nor {$nor:[{conditional 1},{conditional 2},…] } Used to indicate that more than one condition is not met
$and {$and:[{conditional 1},{conditional 2},…] } Indicates that multiple conditions are met at the same time, which is equivalent to and in a logical relationship
$or {$or:[{condition 1},{condition 2},… } Represents the fulfillment of one of multiple conditions, equivalent to or in a logical relationship

1. MongoDB uses find() to query documents

Db.collection_name. Find ({field1:1, field1:1, field1: 0}, field1:1, field1: 0}). Db.shops. Find ({goods_id:4},{_id:0}). Pretty () Db.shop. find({cat_id:{$nin:[3,11]}},{_id:0,cat_id:1}) $nor db.shop.find({cat_id:{$nin:[3,11]}},{_id:0,cat_id:1}) $nor Db. Shop. The find ({$nor: [{cat_id, 3}, {} cat_id: 11]}, {cat_id _id: 0:1}) example 3: Take the item whose price is greater than 100 and less than 300 or greater than 4000 and less than 5000 db.shop.find({$or:[{$and:[{shop_price:{$gt:100}},{shop_price:{$lt:300}}]},{$and:[{shop_price:{$gt:4000}},{shop_price:{$l t:5000}}]}]}).pretty()Copy the code

7. MongoDB updates documentation

MongoDB uses the update() method to update documents in the collection

Grammar:

Db.coll_name. Update ({query expression}, -- update who {update content}, {optional})Copy the code

1. If it is a document, the new document replaces the old document. —– document replacement is not an update

Example: For an item with goods_id 1, you want shop_price 3000

db.shop.update({goods_id:1},{shop_price:3000})
Copy the code

2. If you want to do the update field operation, you must do it with the update expression

Update expression:

$set changes the value of a column:

Example: Change the price of an item with goods_id 4 to 2000

Example: the nominal price of an item with goods_id 4 is 1000, cat_id is changed to 4, goods_number:1

db.shop.update({goods_id:4},{$set:{shop_price:3000}})
db.shop.update({goods_id:4},{$set:{shop_price:1000,cat_id:4,goods_number:1}})
Copy the code

$unset Deletes a column:

Example: Delete the add_time field from column 3

db.shop.update({cat_id:3},{$unset:{add_time:1}})
Copy the code

By default, only one data is operated on

$rename Rename a column:

Example: Change the cilck_count of a document whose goods_number is less than 5 to count_click– all replacements

db.shop.update({goods_id:{$lt:5}},{$rename:{click_count:'count_click'}},{multi:true})
Copy the code

Multi :true: updates all matches, default is false, that is, updates one

$inc grows a column

Example: Reduce the price of an item whose goods_id is 11 by 100

db.shop.update({goods_id:11},{$inc:{shop_price:-100}})
Copy the code

Optional: {upsert:true} This parameter means whether to insert objNew if there is no update record. True is inserted, and the default is false

Db. Shop. Update ({goods_id: 1}, {$set: {goods_name: 'huawei p30, goods_number: 100}}, {upsert: true}) Db. Shop. The find ({goods_name: 'huawei p30})Copy the code

Update with upsert:true allows you to insert data when it does not exist and update data when it does exist

MongoDB deletes documents

The MongoDB remove() function is used to remove data from the collection

Grammar:

db.shop.remove({goods_id:1})
Copy the code

9, MongoDB limit, skip, sort, count methods

Limit: If you need to read a specified number of records from MongoDB, you can use MongoDB’s limit method. The limit() method takes a numeric argument specifying the number of records to read from MongoDB.

Limit the grammar:

db.shop.find().limit(5)
Copy the code

Skip: In addition to reading a specified amount of data using the limit() method, we can skip a specified amount of data using the skip() method, which also takes a numeric parameter as the number of records to skip

Skip the grammar:

db.shop.find().limit(5).skip(5)
Copy the code

Sort: sort the data in MongoDB by sort(), sort() by 1 and -1, where 1 is in ascending order and -1 is in descending order.

Sort syntax:

db.shop.find().sort({goods_id:1})
Copy the code

Count: Use the count() method to count data in MongoDB

The count grammar:

db.shop.find().count()
Copy the code

X. Import and export of MongoDB

Mongoexport -d database name -c set -f export field -q query conditions -o input filename (path) abc.json --type CSV type # export json mongoexport -d shop -c Mongoexport -d shop -c shop -f goods_name,shop_price -o goods. CSV --type CSV # mongoexport -d shop -c shop -f goods_name,shop_price -o goods mongoexport -d shop -c shop -q {shop_price:{$gt:1000}} -f goods_name,shop_price -o goods.csv --type csvCopy the code