“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The basic usage of Mongo was covered in the last two articles, Aggregator Pipes, Mongo Installation, and Basics, but it’s still not very convenient to use it native in a Node environment.

Using the mongo

Regardless of the code environment, if you need to connect to a database, you need to use the database driver. (The following code uses Node as an example). In the Node environment, the mongodb driver is called mongodb, but this driver also has a defect, the model verification is not very good, we generally use the library Mongoose to determine the model, verify parameters, etc

Mongoose official website: mongoosejs.com/

Mongoose: www.mongoosejs.net/

mongoose

Mongoose in the node environment is similar to se’ Sequelize ‘connected to MySQL, which is used to define the model, verify whether the rules meet the conditions, and then map the model to the corresponding database.

Relationship between Mongoose and mongodb

  • mongooseYou have your own style andapi
  • mongooseInternal is still usedmongodbOfficial driver, to operate mongo database.
  • mongooseschemaStructure, which describes what fields are in some data, what type each field is, constraints on each field, etc

use

Since it is a driver, it must be installed, the installation method is also very simple, such as: NPM install Mongoose or YARN add Mongoose

Environmental requirements

After installing the driver, you need to ensure that you have the Mongo service running locally. The check method is as follows: win + r –> services.msc

The service needs to be enabled in order to use Mongoose to connect and manipulate the database.

Connecting to a Database

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test1');
var db = mongoose.connection;
db.on('error'.function(){
  console.log('Connection failed, please check if mongo service is started')}); db.on('open'.function() {
  console.log('Can access database properly')});Copy the code

If your service is not started, you will get the following result:

The normal result is this:

Through the above code, you and I may have a question, why mongoose does not need a user name and password to connect to the database? We know that databases are created dynamically, but what about usernames and passwords? Read on with questions:

I took this question to Baidu, plus their own understanding of this aspect, came to the following conclusion. Mongo itself database is dynamic, people simply do not know which database you are connected to, naturally in terms of authority is certainly empty, how to reflect it? As follows:

On Robot 3t, you can see that the user name and password in the permission are empty. As long as you start the service, you can create the database arbitrarily and manipulate the data arbitrarily. This is a little awkward! So in the formal development, we must bear in mind the need to do a good job of data permissions, otherwise everyone can operate the database, which is a very dangerous thing.