A preliminary study on the use of MongoDB in react

MongoDB introduction and installation

What is MongoDB

  • Comes from the English word "Humongous", which means "huge" in Chinese
  • Open source database for document storage
  • Written in C++, supports multi-language connection

Why use MongoDB

  • Good performance (memory computing)
  • Large-scale data storage (scalability)
  • Reliable and secure (local replication, automatic failover)
  • Convenient storage of complex data structures (Schema Free)

For more exciting content, please search for " " on WeChat前端爱好者 , and click me to view . '

MongoDB Download

It can be downloaded from the MongoDB official website [https://www.mongodb.com/zh-cn], and supports common platforms (Windows, Linux, OSX)

Download address: https://www.mongodb.com/try/download/community

MongoD8 Compass subsequent separate installation:

Because of direct installation, it may cause stuck [MongoD8 Compass, you need to climb over the wall to install]

MongoDB installation complete

  • Configure environment variables
  • Related commands
    • mongod -version // Check the version number of mongodb

MongoDB configuration environment variables under Windows

Step 1 : Right-click to open "This PC" , click Properties , and the system settings will be displayed --> Then click Advanced System Settings , and the environment settings panel will be displayed.

Step 2 : Enter advanced system settings --> enter environment variables --> open Path, find the variable named Path in the system variables and double-click to enter

Step 3 : Create a new environment variable: add it at the end of the variable value D:\mongodb\bin\(according to the specific computer location configuration), save and exit, and you are done!

Step 4 : Restart the terminal test

MongoDB Compass

In the process of using MongoDB, if you only rely on the command line to operate the MongoDB database, the efficiency is not high and it is inconvenient to view.

Therefore, a visual management tool provided by MongoDB's official website is called MongoDB Compass , which integrates functions such as creating databases, managing collections and documents, running temporary queries, evaluating and optimizing queries, performance charts, and building geographic queries, which is very convenient.

Download address: https://www.mongodb.com/products/compass

Select the corresponding version to download:

MongoDB Compass uses

Connect to MongoDB Server

If you did not set an account and password when installing MongoDB locally, just click Connect and use the default connection to log in to the local MongoDB.

If the account and password are set, or you want to connect to the remote MongoDB, click Advanced Connection Optionsto enter the required 选项面板connection configuration.

MongoDB Compass main interface

create database


Tip: When Compass creates a database, a collection must be created at the same time, so a collection name is also entered in the above figure.

Manipulating data tables (collection operations)

Mainly include: create new data, modify data, delete data, import data, export data table, etc.

Query data

Use mongoose to connect to the database

Assuming you have already created your project utilizing koa scaffolding .

If you want to know how to create a project using koa scaffolding , please check: Talking about Koa this article.

Create a database with MongoDB Compass

View Results

Use mongoose to connect to the database

mongoose-introduction

mongoose is a library that nodeJS provides to connect to mongodb.

In addition, there are mongoskin and mongodb (officially produced by mongodb).

I still prefer mongoose, because it follows a template method that can automatically process the data you enter.

Interested students can go to Mongoose official website to see.

Mongoose Official Documentation

  • mongoose - Version 6.1.2 【https://mongoosejs.com/】
  • mongoose - Version 5.0.15 Chinese version 【http://www.mongoosejs.net/】

install mongoose

To use mongoose you need to have nodeJS and mongodb database, download mongoose:

npm install mongoose --save

In the project, create a new db folder , and create a new index.js file for connecting to the database.

server\db\index.js

const mongoose = require('mongoose');

module.exports = ()=>{
    mongoose.connect('mongodb://localhost:27017/adminPublish',{
        useUnifiedTopology: true,
        useNewUrlParser: true
    })
    .then(()=>{
        console.log('数据库连接成功')
    }).catch(err=>{
        console.error('数据库连接失败',err)
    })
}

In the app.js entry file, introduce the database connection and start the database connection.

// app.js
... 
const MongoConnect = require('./db/index') // 引入文件 

MongoConnect() // 链接数据库
...

details as follows:

Lesson of the day: MongoDB common commands

  • mongo: Start the MongoDB client.
  • show dbs: Displays all databases.
  • show collections: Shows all collections in the selected database
  • use <db_name>: use the specified database.
  • db..find <collection_name>(): Finds all documents in the specified collection.
  • db..findOne <collection_name>( <query>): Finds a document in the specified collection.
  • db..insert <collection_name>( <document>): Insert a document in the specified collection.
  • db..insertOne <collection_name>( <document>): Insert a document in the specified collection.
  • db..update <collection_name>( <query>, <update>): Updates one or more documents in the specified collection.
  • db. <collection_name>.updateOne( <query>, <update>): Updates one or more documents in the specified collection.
  • db..remove <collection_name>( <query>): Deletes one or more documents in the specified collection.
  • db..deleteOne <collection_name>(): delete the specified document
  • db..aggregate <collection_name>(): Performs an aggregate operation on a collection

These commands are just a small subset of the MongoDB commands that are fundamental to working with MongoDB.

Guess you like

Origin blog.csdn.net/BradenHan/article/details/131160649