model.mongo(Mongo數(shù)據(jù)庫模型)

2021-09-17 14:33 更新

think.model.mongo 繼承類 think.model.base

使用 ES6 的語法繼承該類
export default class extends think.model.mongo {
  getList(){

  }
}
使用普通方式繼承該類
module.exports = think.model("mongo", {
  getList: function(){

  }
})

屬性

model.indexes

設(shè)置字段索引,數(shù)據(jù)操作之前會(huì)自動(dòng)創(chuàng)建索引。

export default class extends think.model.mongo {
  init(...args){
    super.init(...args);
    //配置索引
    this.indexes = { 

    }
  }
}
單字段索引
export default class extends think.model.mongo {
  init(...args){
    super.init(...args);
    //配置索引
    this.indexes = { 
      name: 1
    }
  }
}
唯一索引

通過 $unique 來指定為唯一索引,如:

export default class extends think.model.mongo {
  init(...args){
    super.init(...args);
    //配置索引
    this.indexes = { 
      name: {$unique: 1}
    }
  }
}
多字段索引

可以將多個(gè)字段聯(lián)合索引,如:

export default class extends think.model.mongo {
  init(...args){
    super.init(...args);
    //配置索引
    this.indexes = { 
      email: 1
      test: {
        name: 1,
        title: 1,
        $unique: 1
      }
    }
  }
}

model.pk

主鍵名,默認(rèn)為 _id,可以通過 this.getPk 方法獲取。

方法

model.where(where)

mongo 模型中的 where 條件設(shè)置和關(guān)系數(shù)據(jù)庫中不太一樣。

等于判斷
export default class extends think.model.mongo {
  where1(){
    return this.where({ type: "snacks" }).select();
  }
}
AND 條件
export default class extends think.model.mongo {
  where1(){
    return this.where({ type: "food", price: { $lt: 9.95 } }).select();
  }
}
OR 條件
export default class extends think.model.mongo {
  where1(){
    return this.where({
     $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
    }).select();
  }
  where2(){
    return this.where({
     type: "food",
     $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
   }).select();
  }
}
內(nèi)嵌文檔
export default class extends think.model.mongo {
  where1(){
    return this.where( {
      producer:
        {
          company: "ABC123",
          address: "123 Street"
        }
    }).select();
  }
  where2(){
    return this.where({ "producer.company": "ABC123" } ).select();
  }
}
IN 條件
export default class extends think.model.mongo {
  where1(){
    return this.where({ type: { $in: [ "food", "snacks" ] } }).select();
  }
}

更多文檔請見 https://docs.mongodb.org/manual/reference/operator/query/#query-selectors。

model.collection()

  • return {Promise}

獲取操作當(dāng)前表的句柄。

export default class extends think.model.mongo {
  async getIndexes(){
    let collection = await this.collection();
    return collection.indexes();
  }
}

model.aggregate(options)

聚合查詢。具體請見 https://docs.mongodb.org/manual/core/aggregation-introduction/。

model.mapReduce(map, reduce, out)

mapReduce 操作,具體請見 https://docs.mongodb.org/manual/core/map-reduce/。

model.createIndex(indexes, options)

  • indexes {Object} 索引配置
  • options {Object}

創(chuàng)建索引。

model.getIndexes()

  • return {Promise}

獲取索引。

文檔地址:https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_model_mongo.md

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號