[后端開發(fā)]Mongoose在Node中的使用

1.0 Mongoose介紹

mongoose是Node環(huán)境下異步操作數(shù)據(jù)的對象數(shù)據(jù)模型,僅限于Node環(huán)境下使用,是基于MongoDB的驅(qū)動.

1.0.0連接數(shù)據(jù)庫
const mongoose = require("mongoose");
// useNewUrlParser設(shè)個屬性會在url里識別用戶所需要的db,升級之后必須指定
mongoose.connect("mongodb://admin:123456@127.0.0.1:27017/admin",{useNewUrlParser:true},(err)=>{
    if(err){
        console.log(err+"數(shù)據(jù)庫連接失敗");
    }
    console.log("數(shù)據(jù)庫連接成功");
});

module.exports = mongoose;
1.0.1 定義Schema

schema 是mongoose 中會用到的一種數(shù)據(jù)模式,可以理解為表結(jié)構(gòu)的定義;每個schema 會映射到mongodb 中的一個collection,它不具備操作數(shù)據(jù)庫的能力,同時可以在Schema中定義數(shù)據(jù)校驗(yàn),默認(rèn)值,字段名,字段類型,默認(rèn)修飾符等特性。

const ArticleSchema = mongoose.Schema({
        order_id:String,
        uid:Number,
        publishTime:String,
        all_article:Number,
        all_num:{
            type:Number,
            default:1
        }
});
//三個參數(shù)的含義,model名,schema名,操作的數(shù)據(jù)集合
const ArticleModel= mongoose.model("Article",ArticleSchema ,"articles");
module.exports = ArticleModel;
  • required: required validator 該字段必不可少
  • default: 當(dāng)不指定字段值的時候默認(rèn)的值
  • select: boolean, specifies default projections for queries
  • validate:validator function 添加校驗(yàn)函數(shù)
  • get: 為這個字段定義一個getter函數(shù),using Object.defineProperty().
  • set: 為這個字段定義一個setter函數(shù),using Object.defineProperty().
  • alias: 為這個字段定義個虛擬的別名, mongoose >= 4.10.0 only. virtual
    除了以上幾個字段屬性,接下來還有index,String,Number,Date等字段特有的屬性

Schema Type類型

1.0.2 定義model

model 是由schema 生成的模型,可以對數(shù)據(jù)庫的操作

ArticleModel.數(shù)據(jù)操作符({....},(err,doc)=>{
    if(err){
        console.log("......失敗"+err);
        return 
    }
    // console.log(doc);
});

2.0 mongoose的數(shù)據(jù)庫連接

  1. 安裝
npm i mongoose --save

2、引入mongoose 并連接數(shù)據(jù)庫

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
//在開啟驗(yàn)證的情況下需要用如下方式登錄連接
mongoose.connect('mongodb://username:password@localhost:27017/DB_NAME');

mongoose的其他數(shù)據(jù)操作和mongoDB一樣,這里不做記錄

3.0 Schema字段屬性補(bǔ)充

const OrderSchema = mongoose.Schema({
        field_name:{
            type:Number,
            default:1
        }
});

在field_name屬性中除了上面Schema中說到的還有:

  • Index相關(guān)
    • index:是否設(shè)置該字段為索引
    • unique:是否是唯一索引
    • sparse :稀疏索引(稀疏索引(或者稱間隙索引)就是只包含有索引字段的文檔的條目,跳過索引鍵不存在的文檔) 博文介紹
  • String相關(guān)
    • lowercase:是否返回調(diào)用.toLowerCase()的值
    • uppercase: 是否返回調(diào)用.toUpperCase()的值
    • trim: 是否去空格
    • match: RegExp, 創(chuàng)建一個 validator 用于數(shù)據(jù)校驗(yàn)
    • enum: 枚舉的字符串?dāng)?shù)組,(字段值必須在這個枚舉數(shù)組中) validator
    • minlength: 最小長度校驗(yàn)
    • maxlength最大數(shù)據(jù)長度校驗(yàn)
  • Number相關(guān)
    • max字段必須小于改屬性值
    • min 字段必須大于改屬性值
  • Date相關(guān)
    • max:最大日期
    • min:最小日期

4.0 自定義數(shù)據(jù)校驗(yàn)&&修飾符

4.0.0 自定義校驗(yàn)器

自定義校驗(yàn)器和上面提到的match屬性非常類似,可以是正則也可以是其他返回boolean值的表達(dá)式,validate針對所有數(shù)據(jù)類型

const AriticleSchema = mongoose.schema({
  field_name:{
    validate:function(field_name){
          //這里可以是對該字段的任意數(shù)據(jù)校驗(yàn)內(nèi)容
          return field_name.length>5?true,false;
    }
  }
)
4.0.1 自定義修飾符Setter&Getter

除了mongoose 以上內(nèi)置的修飾符以外,我們還可以通過set(建議使用) 修飾符在增加數(shù)據(jù)的時候?qū)?shù)據(jù)進(jìn)行格式化,此時的數(shù)據(jù)將會被存儲到數(shù)據(jù)庫。也可以通過get在實(shí)例獲取數(shù)據(jù)的時候?qū)?shù)據(jù)進(jìn)行格式化,此時的數(shù)據(jù)并不會被存儲到數(shù)據(jù)庫

const AriticleSchema = mongoose.schema({
  field_name:{
         set(field_value){//field_value是field_name對應(yīng)的值
              return "field_name:"+field_value
          }
        get(field_value){
              return "my title is :"+field_value;
        }
  }
);


//在實(shí)例中調(diào)用article.title(假如field_name此時為title)
var article = new ArticleSchema({
    title:"文章的標(biāo)題頁面"
});
console.log(article.title)//my title is :文章的標(biāo)題頁面
但是通過ArticleModel.find({}....)查找到的title仍然是  “文章標(biāo)題頁面”

5.0 自定義實(shí)例&&靜態(tài)方法

以下是官網(wǎng)提供的幾個靜態(tài)方法

Mongoose models provide several static helper functions for CRUD operations. Each of these functions returns amongoose Query object.

// 靜態(tài)方法
ArticleSchema.statics.findByAid=function(aid,cb){
//這里的this指向ArticleModel
this.find({"_id":aid},function(err,docs){
    cb(err,docs)
})
}
// 實(shí)例方法
ArticleSchema.methods.showContent= function(){
    console.log('這是一個實(shí)例方法');
    console.log(this.content);//這里的this指向單個ArticleModel實(shí)例對象
};

6.0 多表的數(shù)據(jù)關(guān)聯(lián)

6.0.0 一對一的數(shù)據(jù)表關(guān)聯(lián)
ArticleModel.aggregate([
   {
       $lookup:{
           from:"articleCagetory",//關(guān)聯(lián)的表
           local_field:"cid",COLLECTION_NAME//的關(guān)聯(lián)字段
           foregin_field:"categoryId",//articleCagetory的關(guān)聯(lián)字段
           as:"category"http://articleCagetory關(guān)聯(lián)之后的名稱
       }
   }
],function(err,docs){
  ...
})
6.0.1 多表之間的關(guān)聯(lián)

多表的關(guān)聯(lián)如果使用aggregate可以在上段代碼中通過疊加多個$lookup實(shí)現(xiàn)多表的關(guān)聯(lián),這里不做過多演示

使用populate使用多表關(guān)聯(lián)
1定義Schema,外鍵指向關(guān)聯(lián)表的model名(即mongoose.model(,,,))的第一個參數(shù)

var ArticleSchema = mongoose.Schema({
      aid:Number,
      cid:{
          ref:"articleCategoryModel"http://文章分類表Model
      },
      author_id:{
            ref:"authorModel"http://作者表model
    }
    ..........
})

2 引入表的model,并進(jìn)行關(guān)聯(lián)

const ArticleCategoryModel = require("./model/ArticleCategoryModel.js");
const AuthorModel = require("./model/AuthorModel.js"));
const ArticleModel = require("./model/ArticleModel.js"));

ArticleModel
  .find({})
  .populate("cid")
  .populate("author_id")
  ....//通過populate("外鍵字段")可以關(guān)聯(lián)多個表
  .exec(function(err,docs){

})

7.0 Mongoose字段過濾

不在找到的數(shù)據(jù)中顯示name字段

AuthorModel
.find()
.select('-name')
.exec(function(err, doc){
})

表關(guān)聯(lián)過濾搜索

ArticleModel
    .findById(id)
    .populate({
        path: 'author_id',//在ArticleMode中的某個字段
        select: 'name age'//要在AuthorModel中搜索的顯示的關(guān)鍵字
    }).exec(function (err, result){
        ...
    })

模糊搜索

var kw = ctx.params.keyword
AuthorModel
.find({$or:[{name:kw },{title:kw}]})
.exec(function(err, doc){
})

8.0 數(shù)據(jù)備份與還原

數(shù)據(jù)導(dǎo)出

首先數(shù)據(jù)庫備份: mongodump -h IP --port 端口 -u=用戶名 -p=密碼 -d 數(shù)據(jù)庫 -o 文件存在路徑  --authenticationDatabase admin

數(shù)據(jù)導(dǎo)入

mongorestore -d 127.0.0.1:27017 -d koademo -u=admin -p=123456   C:\Users\hp\Desktop\db\eggcms   --authenticationDatabase admin

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容