Node.js (three, MongoDB database)

1. Database overview and environment construction

1.1. Why use a database

  • The data in the dynamic website is stored in the database.
  • A database can be used to persistently store user information collected by the client through forms.
  • The database software itself can efficiently manage the data.

1.2. What is a database

A database is a warehouse for storing data, which can store data in an orderly manner. It is software independent of language and can be operated through API.
Common database software includes: mysql, mongoDB, oracle.

1.3 What is MongoDB

  • MongoDB is a database based on distributed file storage, written in C++ language. It aims to provide scalable high-performance data storage solutions for WEB applications.
  • Features of mongoDB : high performance, easy to deploy, easy to use, and it is very convenient to store data . The provided API is developed by javascript; the data stored in the database is in the form of json objects. Easier to learn than the other two.

1.4, MongoDB database download and installation

https://www.runoob.com/mongodb/mongodb-osx-install.html

  • Attached is the download and installation steps for the Mac system: run directly in the tool after the command:
1)、 xcode-select --install2)、 brew tap mongodb/brew3)、 brew install mongodb-community@5.0
  • Attached Homebrew download method: /bin/zsh -c “$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)”
  • Attached xcode-select installation:
命令执行:xcode-select --install
报错:xcode-select: error: command line tools are already installed, use "Software Update" to install updates
解决方法:$ rm -rf /Library/Developer/CommandLineTools
$ xcode-select --install
还是报错:
$ sudo  rm -rf /Library/Developer/CommandLineTools
$ sudo  xcode-select --install

1.5. Concepts related to MongoDB database

A database software can contain multiple data warehouses, each data warehouse can contain multiple data collections, and each data collection can contain multiple documents (specific data).
insert image description here

  • Multiple databases can be created in one mongodb.
  • The default database of MongoDB is "db", which is stored in the data directory.
  • A single instance of MongoDB can hold multiple independent databases, each with its own collections and permissions, and different databases are also placed in different folders.
  • A collection is a MongoDB document group, similar to a table in an RDBMS (Relational Database Management System) .
  • Collections exist in the database, and collections have no fixed structure, which means that you can insert data of different formats and types into collections, but usually the data we insert into collections will have certain relevance.
  • A document is a key-value pair (ie BSON). MongoDB documents do not need to set the same fields, and the same fields do not need to have the same data type, which is very different from relational databases and is also a very prominent feature of MongoDB. A simple documentation example is as follows: {"name": "lgg", "hobby": ["sleep", "running"]} .
  • MongoDB data types
    insert image description here

1.6. Common commands of MongoDB database

1. Help View command prompt
help
db.help()
db.test.help()
db.test.find().help()

2. Create/switch database
use music

3. Query the database
show dbs

4. View the currently used database
db/db.getName()

5. Display the current DB status
db.stats()

6. View the current DB version
db.version()

7. View the link machine address of the current DB
db.getMongo()

8. Delete the database
db.dropDatabase()

1.7. Start MongoDB

1.7.1, Windows system:

  • Start the MongoDB service: $ net start MongoDB or
    $ mongod --dbpath d:/data/db
    $ mongo
  • Close the MongoDB service: $ net stop MongoDB
  • Remove MongoDB service: $ C:\mongodb\bin\mongod.exe --remove

1.7.2, mos system:

  • Start the service:
    $ mongod --config /usr/local/etc/mongod.conf
    $ mongo
    or mongodb:
    mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo .log --fork
    (–dbpath sets the data storage directory;–logpath sets the log storage directory;–fork runs in the background)
  • If you don't want to run on the backend, but view the running process on the console, you can directly set the configuration file to start: mongod --config /usr/local/etc/mongod.conf
  • Check whether the mongod service is started: $ ps aux | grep -v grep | grep mongod
  • After startup, we can use the mongo command to open a terminal:
    $ cd /usr/local/mongodb/bin
    $ ./mongo

2. MongoDB basic operation (command line)

2.1, database operation

db // 查询当前数据库
show dbs // 查询所有的数据库
use lggFirst // 创建/切换数据库
db.dropDatabase() // 删除数据库
  • P.S. in MongoDBNo need to explicitly create a database, if the database being used does not exist, ==MongoDB will be created automatically.
  • Create:
    use database name; // If no collection is created, it will not be displayed in the database list (show dbs)
  • Delete:
    db.dropDatabase() // Delete in the currently open database.

2.2. Collection operations

创建集合
db.createCollection('名字')
获取当前db所有集合
db.getCollectionNames()

Attachment: In the code: creating a collection is divided into two steps, one is tocollection setting rules,two iscreate collection, Create an instance of the mongoose.Schema constructor to create a collection.

2.3. Document operation

1、插入数据
 // 插入一条数据
db.users.insertOne({
    
    })
db.lgg.insert({
    
    name: 'lgg'})
 // 插入多条数据
db.lgg.insert([{
    
    ···}, {
    
    ···}])
db.users.insertMany([{
    
    }, {
    
    }])
db.lgg.save( ) // 与上面一样
3、修改数据
// 1、如果第二个参数是一个对象,后面两个参数无效。
// 2、如果第二个参数是通过$set设置的话,后两个参数才有效。
db.lgg.update({
    
    name: 'lgg'}, {
    
    $set: {
    
    age: 88}}, true, true)
// 3、第三个参数:true如果查询不到,就创建;false如果查询不到就什么都不做。
// 4、第四个参数:true更新多条;false更新一条。
db.lgg.update({
    
    name: 'lgg'}, {
    
    $inc: {
    
    age: 88}}, true, true)
// 5、如果使用updateMany, 就不需要传递后两个参数第二个了
db.users.updateMany({
    
    name: 'lg'}, {
    
    $set: {
    
    name: 'l'}})
3、删除数据
db.lgg.remove({
    
    name: 'lgg'})
4、查询数据
// 1、查询所有记录
db.lgg.find()
// 2、查询去重后数据
db.lgg.distinct('age')
// 3、查询age=10的记录
db.lgg.find({
    
    age: 10})
// 4、查询age>10的记录
db.lgg.find({
    
    age: {
    
    $gt: 10}})
// 5-7、$lt小于;$gte大于等于;$lte小于等于;
// 8、查询大于等于10并且小于等于80
db.lgg.find({
    
    age: {
    
    $gte: 10, $lte: 80}})
// 9、正则查询
db.lgg.find({
    
    name: /l/}) // 查询名字中带有l的
// 10、查询指定列name、age数据
db.lgg.find({
    
    }, {
    
    _id: 0,age: 1}) // {}表示查找全部,{name: /l/}表示name中带l的;0表示结果不包括此项,1表示包括.
// 11、排序(1升序;-1降序)
db.lgg.find().sort({
    
    age: 1})
// 12、查询前2条数据
db.lgg.find().limit(2)
// 13、查询2条之后的数据
db.lgg.find().skip(2)
// 14、查询3-4条之间的数据
db.lgg.find().limit(2).skip(2)
// 14、查询3-4条之间的数据且排序
db.lgg.find().limit(2).skip(2).sort({
    
    age: 1}) // 排序与.sort({age: 1})的位置无关。
// 15、or查询。查询age等于10或80的数据
db.lgg.find({
    
    $or: [{
    
    age: 10}, {
    
    age: 80}]})
// 16、查询第一条数据
db.lgg.findOne()
16、查询某个结果集的记录条数
db.lgg.find().count()

3. MongoDB basic operation (code)

3.1, Mongoose third-party package

  • Using Node.js to operate the MongoDB database needs to rely on the Node.js third-party package mongoose.
  • usenpm install mongoosecommand download.

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

// 必须先启动MongoDB,否则MongoDB将无法连接。
const mongoose = require('mongoose');
// // 在MongoDB中不需要显式创建数据库,如果正在使用的数据库不存在,MongoDB会自动创建。
// 返回的是promise对象
mongoose.connect('mongodb://localhost:27017/lggFirst', {
    
     useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('数据库连接成功。'))
.catch(err => console.log(err, '数据库连接失败。'))

3.3, MongoDB addition, deletion, modification and query operations

3.3.1. Create a collection and insert documents into the collection

// 在MongoDB中不需要显式创建数据库,如果正在使用的数据库不存在,MongoDB会自动创建。
const mongoose = require('mongoose');
// 数据库连接 27017是MongoDB数据库的默认端口
mongoose.connect('mongodb://localhost:27017/lggSecond', {
    
    
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => console.log('数据库连接成功'))
    .catch((err) => console.log(err, '数据库连接失败'))

// 创建集合规则:创建Schema构造函数就是在创建集合规则
const courseSchema = new mongoose.Schema({
    
    
    name: String,
    total: Number,
    isOpen: Boolean
})
// 使用规则创建集合:参数一集合名称,参数二集合规则
const Course = mongoose.model('Course', courseSchema) // 在数据库中是courses

// 创建文档
const course = new Course({
    
    
    name: 'lgg',
    total: 3,
    isOpen: true
})
// 将文档插入到数据库中
course.save()

3.3.2. Create document

Creating a document is actuallyInsert data into the collection

  • Method 1:
    1. Create a collection instance.
    2. Call the save method under the instance object to save the data to the database.
  // 创建集合实例
 const course = new Course({
    
    
     name: 'Node',
     total: 11,
     isOpen: true
 });
  // 将数据保存到数据库中
 course.save();
  • Method Two:
// 通过回调函数方式
Course.create({
    
    name: 'Node基础', total: 9, isOpen: true}, (err, doc) => {
    
     
     //  错误对象
    console.log(err)
     //  当前插入的文档
    console.log(doc)
});
// 	或者通过promise方式
Course.create({
    
    name: 'Node基础', total: 9, isOpen: true})
      .then(doc => console.log(doc))
      .catch(err => console.log(err))

3.3.3, MongoDB database import data

  • mongoimport -d database name -c collection name --file The data file to be imported
    For example: mongoimport -d lggSecond -c users --file ./user.json
    Before executing the command, you need to find the installation directory of the mongodb database and set the installation directory The bin directory under is placed in the system environment variable

3.3.4. Query documents

// 使用规则创建集合:参数一集合名称,参数二集合规则
const User = mongoose.model('User', usersSchema) // 在数据库中是users

// 1、条件为空则查询全部
User.find()
.then(result => console.log(result)) // 返回文档集合

// 2、查询_id为5c09f267aeb04b22f8460968的文档
User.find({
    
    
    _id: '5c09f267aeb04b22f8460968'
})
.then(result => console.log(result)) // 虽然只有一条数据但是返回的也是一个数组(find方法返回的都是数组)

// 3、findOne方法返回一条文档,默认返回当前集合中的第一条文档
User.findOne({
    
    _id: '5c09f267aeb04b22f8460968'})
.then(result => console.log(result)) // 返回的是一个对象

// 4、查询年龄大于20且小于40的文档
User.find({
    
    age: {
    
     $gt: 20, $lt: 40 }})
.then(result => console.log(result))

// 5、查询hoppies数组中包含足球的字段(应用:关键字搜索)
User.find({
    
     hobbies: {
    
     $in: ['足球']}})
.then(result => console.log(result))

// 6、选择要查询的字段(如果不想包含前面加-)
User.find().select('name -_id')
.then(result => console.log(result))

// 7、根据年龄字段进行排序(前面加-表示降序)
User.find().sort('-age')
.then(result => console.log(result))

// 8、skip跳过多少条数据;limit限制多少条数据
User.find().skip(5).limit(2)
.then(result => console.log(result))

3.3.5. Delete document

// 使用规则创建集合:参数一集合名称,参数二集合规则
const User = mongoose.model('User', usersSchema) // 在数据库中是users

// // 查找到一条文档并且删除,返回的是删除的文档。如果查询到了多个那么只会删除第一个匹配的文档。
User.findOneAndDelete({
    
    _id: '123'})
.then(result => console.log(result))

// 删除多个文档(如果传入的为空或{}则删除全部),返回一个对象
User.deleteMany({
    
    })
.then(result => console.log(result)) // { n: 2, ok: 1, deletedCount: 2 }

3.3.6. Update documents

// 使用规则创建集合:参数一集合名称,参数二集合规则
const User = mongoose.model('User', usersSchema) // 在数据库中是users

// 更新单个
User.updateOne({
    
     name: '狗蛋'}, {
    
     name: 'lgg' })
.then(result => console.log(result)) // { n: 1, nModified: 1, ok: 1 }

// 更新多个
User.updateMany({
    
    age: 20}, {
    
    age: 18})
.then(result => console.log(result)) // { n: 0, nModified: 0, ok: 1 } { n: 1, nModified: 1, ok: 1 } 

3.3.7. Set validation rules

When creating a collection rule, you can set the validation rules for the current field, and if the validation fails, the input insertion will fail.

  • Get error message: error.errors['field name'].message
// 创建集合规则:创建Schema构造函数就是在创建集合规则
const informationSchema = new mongoose.Schema({
    
    
    name: {
    
    
        type: String,
        require: true,
        minlength: [4, '长度不能小于4'],
        maxlength: [20, '长度不能大于20'],
        trim: true, // 去除两边字符串
    },
    total: {
    
    
        type: Number,
        min: [0, '最小值为0'],
        max: [999, '最大值为999'],
        default: 0, // 默认值
    },
    isOpen: Boolean,
    publishDate: {
    
    
        type: Date,
        default: Date.now, // 默认值
    }author: {
    
    
        type: String,
        validator: v => {
    
     // 返回布尔值true验证成功;false验证失败
            return v && v.length > 4 // v就是传入的值。
        },
        message: '自定义错误信息'
    }
})
// 使用规则创建集合:参数一集合名称,参数二集合规则
const Information = mongoose.model('Information', informationSchema) 

Information.create({
    
    
    name: 'lgggg',
    total: 99,
    isOpen: true,
})
.then(result => console.log(result))
// 获取错误信息:error.errors['字段名称'].message
.catch(err => {
    
    
    // 获取错误信息对象
    const errs = err.errors
    // 循环错误信息对象
    for(var val in errs) {
    
    
        // 将错误信息打印到控制台
        console.log(errs[val].message);
    }
})
/*
required: true 必传字段
minlength:3 字符串最小长度
maxlength: 20 字符串最大长度
min: 2 数值最小为2
max: 100 数值最大为100
enum: ['html', 'css', 'javascript', 'node.js'] // 枚举,只能传这几个值
// 或者
enum: {
    // values: ['html', 'css', 'javascript', 'node.js'],
    // message: '自定义错误信息'
// } 
trim: true 去除字符串两边的空格
validate: 自定义验证器
default: 默认值
*/

3.3.8, set association

usuallyThere is a relationship between data in different sets, For example, the article information collection and user information are stored in different collections, but the article is published by a certain user. To query all the information of the article, including the user who published it, you need to use the collection association.

  • Use id to associate collections
  • Use the populate method to query associated collections
const Author = mongoose.model('Author', new mongoose.Schema({
    
    name: String}))
// 文章集合
const Post = mongoose.model('Post', new mongoose.Schema({
    
    
    title: {
    
    
        type: String
    },
    // 使用ID将文章集合和作者集合进行关联
    author: {
    
    
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Author'
    }
}))

// Author.create({
    
    
//     name: 'lgg'
// })
// Post.create({
    
    
//     author: '6212657009f6a1023efebce8',
//     title: 'lz'
// })
// 联合查询
Post.find()
.populate('author') // 没有这行代码的话author存放的是6212657009f6a1023efebce8,而不是对象
.then((result) => console.log(result))/*[
    {
      _id: 621265df07646d033be1ea14,
      author: { _id: 6212657009f6a1023efebce8, name: 'lgg', __v: 0 },
      title: 'lz',
      __v: 0
    }
  ]*/

Guess you like

Origin blog.csdn.net/weixin_44767973/article/details/122968562