当前位置:网站首页>Mongodb database

Mongodb database

2020-11-08 17:47:31 Qing comes from orchid

1. Database overview and environment building

1.1 Why use a database

  • The data in the dynamic website is stored in the database

  • The database can be used to persist the user information collected by the client through forms

  • Database software itself can manage data efficiently

1.2 What is a database

A database is a warehouse for storing data , Data can be stored in an orderly and classified manner . It's language independent software , Can pass API To operate it . Common database software is : mysql. mongoDB. oracle.

 

 

 

 

 

1.3 MongoDB Download and install the database

Download address : https://www.mongodb.com/download-center/community

1.4 MongoDB Visualization software

MongoDB Visual operation software , It's a way to operate a database using a graphical interface .

 

 

 

 

1.5 Database related concepts

Multiple data warehouses can be included in a database software , Each data warehouse can contain multiple data sets , Every Data sets can contain multiple documents ( Specific data ).

The term interpretative statement
database database ,mongoDB Multiple databases can be created in database software
collection aggregate , A collection of data , It can be understood as JavaScript In the array
document file , A specific piece of data , It can be understood as JavaScript Objects in the
field Field , Attribute names in the document , It can be understood as JavaScript Object properties in

1.6 Mongoose The first 3 Tripartite package

  • Use Nodejs operation MongoDB The database needs to depend on Node.js The first Tripartite package mongoose

  • Use npm install mongoose Command download

1.7 start-up MongoDB

Run in the command line tool net start mongoDB You can start MongoDB, otherwise MongoDB Will not be able to connect .

1.8 Database connection

Use mongoose Provided connect Method to connect to the database .

MongoDB The return is promise object

mongoose.connect('mongodb://localhost/playground')
    .then(() => console.log(' Database connection successful '))
    .catch(err => console.log(' Database connection failed ', err));

When connecting to the database, if you are prompted with the following information , stay content Method to add a second parameter , { useNewUrlParser: true }

(node:15596) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

If you are prompted (node:14524) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

Continue to add { useUnifiedTopology: true }, Separated by commas

//  Introduce third-party modules mongoose
const mongoose = require('mongoose');
// 1、 Connect to database playground, If there is no such database , Automatically created
mongoose.connect('mongodb://localhost/playground', {
       useUnifiedTopology: true,
       useNewUrlParser: true
  })
   // Successful connection
  .then(() => console.log(' Database connection successful '))
   // The connection fails
  .catch(err => console.log(err, ' Database connection failed '));

1.9 Create database

stay MongoDB in There is no need to explicitly create a database , If the database in use does not exist ,MongoDB Automatically created .

版权声明
本文为[Qing comes from orchid]所创,转载请带上原文链接,感谢