Database concepts

A database, in short, can be regarded as an electronic filing cabinet — a place where electronic documents are stored. Users can add, intercept, update and delete data in the files.

A “database” is a collection of data stored together in a manner that can be shared by multiple users, with minimal redundancy, and independent of the application

Database Introduction

  • A repository of data, independent of software outside the language, can be manipulated through an API
  • In the life
    • Storage through warehouses, each warehouse has N shelves, each shelf has N items
  • In the program
    • Data is stored in databases, each database has N tables/sets, and each table/set has N data/documents

Database types

  • Relational database

    1. It follows the unified SQL standard and has similar syntax
    2. There are library and table constraints and so on
    • Oracle, MySql, SQLite, SQL Server, etc
  • Non-relational database (Not Only SQL)

    1. There is no standard
    2. Typically stored as key-value pairs
    3. Faster read times
    • Mongodb (documentation), Redis/Memcache(memory)

Why use a database

  1. Data for a dynamic website is stored in a database
  2. You can persist user information collected by the client through the form
  3. Data can be efficiently managed

Related terms in MongoDB

  • Database: Multiple databases can be created in mongodb

  • -Leonard: A collection of data

  • Document: a concrete piece of data, which can be understood as an object in JS

  • Field: a field that can be understood as an object attribute in JS

    Field — > Document — > Collection — > database

The target

  1. Storage of project data
  2. Practical work can write API interface independently

MongoDB Installation Procedure

  • Linux environment

    1. Download the mongodb installation package (Linux). Use winScp to move the installation package from Windows to Linux.

    2. Connecting a Remote tool to Linux (such as putty)

    3. Go to the installation package directory and decompress the installation package

      tar -zxvf mongodb-linux-x86_64-xxxxx.tgz
      Copy the code
    4. Move the decompression package to the specified directory

      mv mongodb-linux-x86_64-xxxx/ /user/local/mongodb
      Copy the code
    5. Create directories for storing data and logs

      mkdir -p /user/local/mongodb/data /user/local/mongodb/logs
      Copy the code
    6. Starting the MongoDB Service

      /user/local/mongodb/bin/mongod --dbpath=/user/local/mongodb/data --logpath=/user/local/mongodb/logs/mongodb.log --logappend --port=27017 --fork
      Copy the code
    7. Log in later

      /user/local/mongodb/bin/mongo
      Copy the code
  • Windows environment

    1. Download the mongodb (Window) installation package and decompress it

    2. Create a service (move to the installation path of the installation package in the DOS command window)

      Bin /mongod. Exe --install --dbpath disk path --logpath logpathCopy the code
    3. Start the service (run as administrator in mongodb’s bin directory **=)

      net start mongodbnet stop mongodb
      Copy the code
    4. Login (you can also directly configure environment variables in the bin directory **** of mongodb)

      mongo
      Copy the code

MongoDB Basic Operations

Viewing a Database

show databases
Copy the code

Select database

Use Database nameCopy the code

Note: There is no error ** (implicitly created) ** when selecting a database that does not exist in mongodb

View the currently selected database

db
Copy the code

Deleting a Database

db.dropDatabase()
Copy the code

Deletes the currently selected database

See the collection

show collections
Copy the code

Create a collection

Db.createcollection (' Collection name ')Copy the code

Note: Late inserts are implicitly creating collections

Delete the collection

db.xxx.drop()
Copy the code

Add, delete, change and check mongodb documents

The database is mainly used to store project data. After the creation of the database and collection, documents (data) need to be added, deleted, changed and checked

Inserted into the document

  • grammar

    Db. Set name. Insert (JSON data)Copy the code

    If the collection exists, the data is inserted directly; if the collection does not exist, the data is created implicitly

  • Exercise: Insert data into c1 set of test2 database

    use test2db.c1.insert({    
        name: "yuweiqi".age: 18})
    // mongodb assigns each document a globally unique _id key
    Copy the code
Insert ([{name: "zhangsan", age: "zhangsan", age: "zhangsan"); 18}, {name: "zhaosi", age: 19}, {name: "wangwu", age: 20}]Copy the code
  • How can I quickly insert multiple pieces of data

    Because mongodb is implemented by js engine, it supports partial JS syntax

    So we can write a for loop

    for(var i = 0; i < 10; i++) {    
        db.c1.insert({name: "a"+ i, age: i})
    }
    Copy the code

Delete the document

  • grammar

    Db. set name. remove(condition, [, delete a set]) delete a set:trueDelete all matchesfalseDelete an entry (default)Copy the code

Modify the document

  • Basic grammar

    Db. Set name. Update (Condition, new data [, new data, modified multiple data]) New or not: Specifies whether to insert the new data based on the condition. True: Specifies whether to insert the new data. False: Specifies not to insert the new data. True Yes false No (default)Copy the code
  • To upgrade the grammar

    Db. Collection name. Update (condition, {modifier: {key: value}})Copy the code

    Effect modifier | | | | — – | — – | | increasing inc ∣ ∣ ∣ inc increasing | | | increasing inc ∣ ∣ ∣ rename | rename columns | | set ∣ modify column value ∣ ∣ set | modified column value | | set ∣ modify column value ∣ ∣ unset | Delete column |

  • The preparatory work

    for(var i = 1; i <= 10; i++) {    
    db.c2.insert({uname: "zs"+i, ageInsert into c2 collection of test2 database10The dataCopy the code
  • Exercise 1: Change {uname: zs1} to {uname: zs2}

    db.c2.update({uname: "zs2"}, {$set: {uname: "zs22"} })
    Copy the code
  • Exercise 2: Increase or decrease {uname: zs10} age by two years

    Db. C2. Update ({uname: "zs10"}, {$inc: {age: 2}}) to increase the two c2. Update ({uname: "zs10"}, {$inc: {the age: - 2}}) reduce the age of twoCopy the code
  • Exercise 3: insert data: db.c2.insert({uname: “新 龙教主”, age: 888, who: “male”, other: “non-chinese”})

    ““ db.c2.update({uname: “新 长 大教 程”}, {set: {uname: “新 长 大教 程”}}) modify the uname value db.c2.update({uname: “新 长 大教 程”}, {rename: {who: Db.c2. update({inc: {age: 111}}) age add 111 db.c2.update({uname: {age: 111}}) age add 111 db.c2.update({uname: {age: 111}}) age add 111 db.c2.update({uname: {age: 111}}) age }, {unset: {other: true}}) delete the other field

Write multiple modifier db / / at once. The c2. Update ({uname: "l same hierarch"}, {$set: {uname: "l same hierarch"}, $rename: {who: "sex"}, $inc: {age: 111}, $unset: {other: true}}````Copy the code

Query the document

  • grammar

    Age =6 --> {age: 6 --> {age: 6 --> {age: 6 --> {age: 6 --> {age: 6 --> {age: 6 --> 6, sex: "male "} 2 (Optional parameter) Do not write --> Query all fields of eligible data {age: 1} --> Display only age fields of eligible data {age: 1} 0} --> Do not display the age field of eligible data (all other fields are displayed), but no matter how to write system custom _id will be displayedCopy the code

    | condition meaning | | | — – | : — — — – | | gt ∣ than ∣ ∣ gt | greater than | | gt ∣ than ∣ ∣ lt | < | | gte ∣ is greater than or equal to ∣ ∣ gte | greater than or equal to | | gte ∣ is greater than or equal to ∣ ∣ lte | less than or equal to | | Ne ∣ is not equal to ∣ ∣ ne | is not equal to | | ne ∣ is not equal to ∣ ∣ in | contains more than (query) | | $nin | does not contain |

  • Exercise 1: Query all the data

    db.c1.find({})
    Copy the code
  • Exercise 2: Query data for ages greater than 5

    db.c1.find({age: {$gt: 5} })
    Copy the code
  • Exercise 3: Query for ages 5, 8, 10

    db.c1.find({age: {$in: [5, 8, 10]} })
    Copy the code
  • Exercise 4: Look only at age columns (_id not considered)

    db.c1.find({},{age:1})
    Copy the code