1. 程式人生 > 實用技巧 >spring boot: 通過filter過濾器實現中文的簡體繁體字符集轉換(spring boot 2.3.1)

spring boot: 通過filter過濾器實現中文的簡體繁體字符集轉換(spring boot 2.3.1)

一、mongoDB + Node.js 的增刪改查

  mongoDB下載地址 :https://www.mongodb.com/try/download/community

  1、node.js依賴於第三方包 :mongoose操作mongoDB, 所以需要先下載 :npm install mongoose

  2、需要先引入mongoose 第三方包 : const mongoose = require( ' mongoose ' ) ;

  3、連線資料庫:  

    注:node的所有操作資料庫的方法,都返回Promise物件,如果連線的資料庫不存在,會在新增完資料後自動建立

    

1
  mongoose.connect('mongodb://localhost/ 資料庫名 ' ,
{ useNewUrlParser : true, useUnifiedTopology: true }) 2   .then(()=> console.log(' 資料庫連線成功 ')) 3 .catch(()=> console.log(' 資料庫連線失敗 '))

  4、建立集合規則(集合相當於資料表), Schema是一個建構函式,在它的例項物件裡面填寫規則

  例如建立一個使用者表的規則:

    const userSchema = new mongoose.Schema({
         name : String,
        age : Number,
        email : String,
        password : String,
        hobbies : [String]
       // hobbies 裡面存的是一個數組
    })    

  5、建立集合,引數1是集合名(相當於表名),首字母必須大寫,但是在資料庫中顯示為users,引數2是規則,返回的是一個建構函式

1     const User = mongoose.model(' User ', userSchema);

  6、向集合中新增文件(文件就是資料),用集合返回的建構函式的例項物件新增, 預設會新增id 為 _id

1 const user = new User({
2     name: '丫丫' ,
3     age : 37 ,
4     hobbise : ['唱歌','跳舞','好看'],
5     email : "[email protected]" ,
6 password: "yayaya" 7 })

  7、新增的另一種寫法

1 User.create({
2      name: '丫丫' ,
3      age : 37 ,
4      hobbise : ['唱歌','跳舞','好看'],
5      email : "[email protected]" ,
6      password: "yayaya"
7 })
8 .then(result=>console.log(result))
9 .catch(err=>console.log(err))

  8、查詢

  (1)、查詢使用者集合中的所有文件,返回的是查詢到的資料

1    User.find().then(result=>console.log(result))

  (2)、根據條件查詢資料

1   User.find({age: '20'}).then(result=>console.log(result))

  (3)、條件查詢,只查詢一條資料

1    User.findOne({age: '20'}).then(result=>console.log(result))

  (4)、查詢年齡大一20,小於50的資料

1    User.find({age: {$gt: '20', $lt: '50'}}).then(result=>console.log(result))

  (5)、查詢愛好為打豆豆的資料,注:愛好是一個數組

1    User.find({hobbise: {$in: [' 打豆豆 ']}}).then(result=>console.log(result))

  (6)、查詢某些指定欄位,查詢哪些欄位就寫進select中,用空格隔開,不想查詢哪些欄位,就在前面加-,如-_id,預設會查詢 _id

1    User.find().select(' name email -_id ').then(result=>console.log(result))

  (7)、對查詢欄位進行升序,用sort(),降序只要在欄位前加上-

1    User.find().sort(' age ').then(result=>console.log(result))        // 對查詢到的欄位按年齡進行升序排列
2 
3    User.find().sort(' -age ').then(result=>console.log(result))    // 對查詢到的欄位按年齡進行降序排列

  (8)、查詢年齡是10,20,25的選項

1     User.find({age: {$in: [10, 20, 25]}}).then(result=>console.log(result))

  (9)、skip跳過多少條資料,limit限制查詢多少條資料

1    User.find().skip(2).limit(2).then(result=>console.log(then))
   // 查詢到的資料跳過前兩條,剩下的結果中只查詢頭兩條

  9、刪除

  (1)、查詢到一條文件並且刪除,返回刪除的文件,如果查詢匹配到多個文件,那麼將刪除第一條文件

1  User.findOneAndDelete({_id: '5c09f2d9aeb04b22f846096b'}).then(result=>console.log(result));

  (2)、刪除多條文件,返回值是一個物件{n:(刪除多少條文件),ok:1,deletedCount:(刪除的總數)}

1   User.deleteMany({_id: '5c09f267aeb04b22f8460968'}).then(result=>console.log(result));

 10、更新

  (1)、更新集合中的文件(更新一個),第一個引數是查詢條件,第二個引數是修改的值,返回:{n:1,nModified:1,ok:1}

1    User.updateOne({name: '李四'}, {age: '30'}).then(result=>console.log(result));

  (2)、更新集合中的文件(更新多個),第一個引數不寫表示選擇全部

1    User.updateMany({}, {age: '50'}).then(result=>console.log(result));