At present, the database is divided into:
- Relational database:
mysql,sqlserver,Oracle
Note: the data is stored in the data table, which is stored in the computer disk
- Non relational database:
mongoDB,redis
Note: the data is stored in the computer memory, so it is faster to read and write data
To install mongoDB
1. Select another disk for installation path, and do not install disk C
2. Configure the environment variable path and add the path of the bin file of the currently installed mongoDB
3. If the service is not opened, open the corresponding service
4. Check whether the installation is successful and run the mongo command
Operating database commands
1. Enter the database mongo
2. Exit database exit / ctrl+c
3. View all databases show databases / show dbs
4. Create (enter) a database use database name - enter if there is a database, and automatically create and enter if there is no database
Note: show dbs cannot view the newly created empty database. You can view it after adding data
5. View current data db
6. View all data tables under the database show tables
7. Insert a data db into the data table data sheet. insert({name: 'Zhang San', age:20})
8. Insert multiple pieces of data into the data table db data sheet. insert([{name: 'Li Si', age:10},{name: 'Wang Wu', age:30},...])
9. Only one entry can be inserted into the data table, and there is db data sheet. insertOne()
10. Insert multiple entries into the data table data sheet. insertMany()
11. Query all data in the data table db data sheet. find()
12. Delete data table db data sheet. drop()
13. Delete database db dropDatabase()
Note: before entering the current database, use the database name - the database cannot be deleted
Key query command
1. All data db data sheet. find()
2. According to condition db data sheet. find({name: 'Xiao Li'})
3. Less than db data sheet. find({age:{$lt:40}) / / meet all data with age < 40
4. Less than or equal to db data sheet. find({age:{$lte:40}) / / equal is equal to all data satisfying age < = 40
5. Greater than db data sheet. find({age:{$gt:40}) / / meet all data with age > 40
6. Greater than or equal to db data sheet. find({age:{$gte:40}) / / meet all data of age > = 40
7. Not equal to db data sheet. find({age:{$ne:40}) / / meet age= 40 all data
8. Or db data sheet. find({$or:[{name: 'Xiao Li'}, {age:{$lte:20}]}) / / {$or: [{}, {}]} meets all data of Xiao Li or one of them less than 20
Controls which fields are displayed in the queried data
db. data sheet. Find ({age: 20}, {_id: 0}) / / 0 indicates no display, 1 indicates display
Fuzzy query regular
db. data sheet. find({name: / Li /})
Number of statistical data - count()
db. data sheet. find().count()
For example: db data sheet. find({age:{$lt:20}}).count()
Sort - sort()
db. data sheet. find().sort({age:1}) / / 1 for ascending - 1 for descending
Paging - limit() - skip() limit()
db. data sheet. find().limit(n) / / query the first n pieces of data
db. data sheet. find().skip(m).limit(n) / / skip indicates that the subscript starts from m to query the first n pieces of data
Update - one
db. data sheet. updateOne({name: 'Xiao Li'}, {$set:{age:100})
Delete - one
db. data sheet. deleteOne({name: 'Xiao Li'})
Operating database in nodejs
const express = require('express') const app = express() app.listen(3000, () => { console.log('Server startup') }) // To connect to the local mongoDB database using nodejs, you need to use the third-party module mongoose // Using the third-party tool mongoose // Step 1: Download mongoose // Step 2: introduce mongoose const mongoose = require('mongoose') // Step 3: configure connection to local database // Syntax: mongoose Connect ('protocol: / / localhost: database port number / database name ') mongoose.connect('mongodb://localhost:27017/user') // Step 4: set the fields and data types in the tables in the database const userSchema = new mongoose.Schema({ goodsname: String, price: Number, num: Number }) // Step 5: create a data table according to the schema rules you defined // Syntax: mongoose Model ('datasheet ', Datasheet field rules,' datasheet name ') // The third parameter is the data table name. If the third parameter does not exist, the data table name is the first parameter name + s const userModel = mongoose.model('goodlist', userSchema, 'goodlist') // Run nodemon http JS will automatically create a data table according to the above configuration // After configuring and defining the background interface //Product addition interface - use body parser to obtain parameters submitted by post const bodyParser = require('body-parser') const { db } = require('mongodb') app.use(bodyParser.urlencoded({ extended: false })) app.post('/addgoods', (req, res) => { const { goodsname, price, num } = req.body // Add the parameter data transmitted from the front end to the database // Note: all methods provided by userModel return a promise object, so it can be used then().catch() userModel.insertMany({ goodsname: goodsname, price: price, num: num }).then(() => { res.send('Added successfully') }).catch(() => { res.send('Add failed') }) }) // Modify commodity interface app.post('/editgoods', (req, res) => { const { goodsname, price, num, id } = req.body userModel.updateOne({ _id: id }, { $set: { goodsname, price, num } }).then(() => { res.json({ code: 200, msg: "Modified successfully", }) }).catch(() => { res.json({ code: 500, msg: "Modification failed", }) }) }) // Query interface app.get('/goodslist', (req, res) => { console.log(req.query); // Get the entered trade name const { goodsname } = req.query // Find the parameter date in - then in the database, which is the queried data userModel.find({ goodsname: goodsname }).then((date) => { console.log(date); if (date.length > 0) { res.json({ code: 200, msg: 'Yes' }) } else { res.json({ code: 200, msg: 'There is no such item' }) } }) }) // Delete product interface app.get('/deletegoods', (req, res) => { console.log(); const { id } = req.query // Execute database delete command userModel.deleteOne({ _id: id }).then(ret => { if (ret.deletedCount == 1) { res.json({ code: 200, msg: "Delete succeeded" }) } else { res.json({ code: 500, msg: "Deletion failed" }) } }) })