Mongodb database — learning records (1)

Time:2021-4-16

1. Database overview and environment construction

1.1 why use database
  • The data in the dynamic website is stored in the database
  • Database can be used to store user information collected by client through form persistently
  • Database software can manage data efficiently
1.2 what is database
  • Database is the warehouse of storing data, which can store the data orderly and categorically. It is language independent software and can be operated through API.
  • Common database software are: MySQL, mongodb, Oracle.
1.3 download and installation of mongodb database

Download address:https://www.mongodb.com/downl…

1.4 mongodb visualization software

Mongodb database -- learning records (1)

1.5 database related concepts

In a database software can contain multiple data warehouses, in each data warehouse can contain multiple data sets, each data set can contain multiple documents (specific data).

term interpretative statement
database Database, mongodb database software can establish multiple databases
collection Collection, a collection of data, can be understood as an array in JavaScript
document Documents, a specific piece of data, can be understood as objects in JavaScript
field Field, the property name in the document, can be understood as the object property in JavaScript
1.6 mongoose third party package
  • use Node.js Operation of mongodb database depends on Node.js Third party package Mongoose
  • Download with NPM install mongoose command
1.7 start mongodb

Run net start mongodb in the command line tool to start mongodb, otherwise mongodb will not be able to connect.

2. Mongodb database connection

1.8 database connection

Use the connect method provided by mongoose to connect to the database.

mongoose.connect('mongodb://localhost/playground')
     .then(() =>  console.log ('Database connection succeeded '))
     .catch(err =>  console.log ('database connection failed', ERR));
1.9 create database

There is no need to create a database explicitly in mongodb. If the database in use does not exist, mongodb will create it automatically.