Basic Concepts of Database

Basic concepts of Database:

  1. A database is a warehouse that organizes, stores and manages data according to a certain data structure
  2. We write programs that run in memory. Once the program ends or the computer breaks, all the data in the program is lost. So we need to persist some program data to the keyboard, to ensure data security.
  3. Database is a popular choice for large volume data persistence, 1. File 2. Database

Why are databases used to store data?

  • Databases are structured
  • Database can provide a variety of interfaces, so that data processing (add, delete, change and check) fast and convenient
  • Various languages (PHP JSP.net..) Provides a complete interface

### database classification

  1. ROBMS (Relational database) :
    • For example, MySql, SQL Server and Oracle
    • Features are related by table after table
    • Basically all use SQL language to manage database,
  2. Nosql (non-relational database) :
    • There is no notion of rows or columns storing data in JSON classes
    • Collections are tables, documents are rows
    • The friction between standardization and disstandardization.
    • Standardization limits innovation, non-standard words cannot be unified

Features: Use Key values to store data; The logical structure of MongoDB is a hierarchical structure, which is mainly composed of three parts: document, collection and database.

  • Document: consists of key/value pairs like {a:1}; {s: “ABC”}, it is the MongoDB core unit, MongoDB document, equivalent to a row of records in the relational database.
  • Collection: Multiple documents form a Collection, which is equivalent to a table in a relational database.
  • Database: Multiple collections, logically organized together, are databases. A single MongoDB instance supports multiple databases.

Relational database and non-relational database difference: relational database is more structured, operation is not very flexible, Philippine relational database operation is flexible, but not suitable for large data storage, more suitable for micro architecture, the two are complementary to each other

Non-relational database usage aspects:

  1. The data model is simple
  2. A more flexible back-end system is needed
  3. It has high requirements on database performance
  4. A high degree of data consistency is not required

Non-relational databases are mainly suitable for small and micro architectures

Installing the MongoDB database

MongoDB (non-relational database) :

  1. A database system designed for rapid development of Web applications
  2. The design goal is to be minimalist, flexible and often used in the business layer of the Web application stack
  3. Its data model is document-oriented, with a JSON-like structure
  4. So there’s all kinds of JSON in the database, stored as key-value pairs

Installation: Download :www.mongodb.com/ Even numbers are stable releases, odd numbers are development releases that are best downloaded for the 64-bit system version

Configure environment variables:

  1. New –> Install to bin path to copy environment variables
  2. Create the data-> DB folder
  3. Open the command line and enter mongod to start the DB server
  4. Specify the port number and path mongod –dbpath d:\data\db
  5. Set MongDB to system service, data\log create configuration file in bin. Run the following command:
sc.exe create MongoDB binPath= "\" F: \ mongo \ Server \ 3.2 \ bin \ mongod exe \ "- service - config = \" F: \ mongo \ Server \ 3.2 \ mongod CFG \ "" DisplayName= "MongoDB" start= "auto"
Copy the code

Mainly is to make the mongo has been running in memory Reference data: www.cnblogs.com/wzlblog/p/6… www.cnblogs.com/chenlq/p/65…

Use MongoDB to insert commands on the command line

MongoDB components:

  • Database A database is a repository in which collections can be stored
  • Collections: Collections are like arrays in which documents can be stored
  • Documents: Documents are the smallest unit in a database and all we store and manipulate are documents.

Use the database name to go to the specified database. Db Displays the current database. Show Collections Displays all collections in the database

Use the administrator to open CMD and enter mongo to enter the MongoDB environment to execute the MongoDB command. Then use the command line to perform CRUD(add, delete, change, and check) : db.
. Insert (doc)

Select * from student; select * from student; select * from student; Db.student. Insert ({id:”001″,name:” ZNL “,age:18,sex: “male”}) create a new set of data from the current set

Db.< collection>.find();

Such as: db. Student. The find (); Select * from student; select * from student;

Db. Set name. Insert ([{name:"Zhang",age:18,sex:"man"},
    {name:"Zhang",age:18,sex:"man"},
    {name:"Zhang",age:18,sex:"man"}, {id: 18, name:"Zhang",age:18,sex:"man"}]);Copy the code

Insert multiple statements into an array insert can be arbitrary regardless of whether the fields are the same or not if the inserted fields do not exist they will be created automatically

To see more and more operation: www.mongodb.org.cn/manual/

MongoDB visualization tool [NOSQL]

However, has been using the command line operation is very troublesome thing, so it is recommended to use no visualization tools Install visual tools download address: www.mongodbmanager.com/download

Use visual tools

Visual tool lookup function: Insert (insert) statement:

Insert ([{id:1,name:"znl",age:18},
{id:1,name:"znl",age:18},
]);
Copy the code

Db.student (set name).find();

Db.student. find({“_id” : ObjectId(” 5bd01c825f0d528d36a2C06f “)}); Db.student. find({age:18,name:” student “}); Select * from db. Student. FindOne ({age:18,name:” student “}); select * from db. Example Query a specified piece of data

Operator – Query how many pieces of data there are: db.student.find().count(); Or the student. The find (). The length (); Returns the total number of items of data

Db.student.find ({name:” zhang3 “}).length(); Returns the number of entries that match name:” Joe”

Database update command

Update existing field data:

db.student.update({"name":"Zhang"}, {$set:{
      name:"Liu five",
      age:45
}});
Copy the code

$set = $set; $set = $set; $set = $set

Delete a field:

db.student.update({"name":"Zhang"}, {$unset:{
    age:1
}});
Copy the code

Query name: a column of data and delete its age field

Modify multiple:

db.student.updateMany({"sex":"man"}, {$set:{
    aihao:"Play the game"
}});
Copy the code

$set: $set: $set: $set: $set: $set: $set: $set: $set

Method 2:

db.student.update({name:"Zhang"}, {$set:{
      name:"Fifty"
}}, {
     multi:true
});
Copy the code

Reference: docs.mongodb.com/manual/tuto…