[TOC=2,4]
`think.model.mongo`?繼承類?[think.model.base](https://thinkjs.org/zh-CN/doc/2.0/api_model.html)。
##### 使用 ES6 的語法繼承該類
~~~
export default class extends think.model.mongo {
getList(){
}
}
~~~
##### 使用普通方式繼承該類
~~~
module.exports = think.model("mongo", {
getList: function(){
}
})
~~~
### 屬性
#### model.indexes
設置字段索引,數據操作之前會自動創建索引。
~~~
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}
}
}
}
~~~
##### 多字段索引
可以將多個字段聯合索引,如:
~~~
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
主鍵名,默認為?`_id`,可以通過?`this.getPk`?方法獲取。
### 方法
#### model.where(where)
mongo 模型中的 where 條件設置和關系數據庫中不太一樣。
##### 等于判斷
~~~
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();
}
}
~~~
##### 內嵌文檔
~~~
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](https://docs.mongodb.org/manual/reference/operator/query/#query-selectors)。
#### model.collection()
* `return`?{Promise}
獲取操作當前表的句柄。
~~~
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/](https://docs.mongodb.org/manual/core/aggregation-introduction/)。
#### model.mapReduce(map, reduce, out)
mapReduce 操作,具體請見?[https://docs.mongodb.org/manual/core/map-reduce/](https://docs.mongodb.org/manual/core/map-reduce/)。
#### model.createIndex(indexes, options)
* `indexes`?{Object} 索引配置
* `options`?{Object}
創建索引。
#### model.getIndexes()
* `return`?{Promise}
獲取索引。
文檔地址:[https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_model_mongo.md](https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_model_mongo.md)
- 快速入門
- 介紹
- 創建項目
- 項目結構
- 代碼規范
- 升級指南
- 進階應用
- 模塊
- 控制器
- 視圖
- 配置
- 路由
- 模型
- 介紹
- 事務
- 關聯模型
- Mysql
- MongoDB
- SQLite
- Adapter
- 介紹
- Cache
- Session
- WebSocket
- Template
- 擴展功能
- thinkjs 命令
- 靜態資源訪問
- Middleware
- Service
- Cookie
- 錯誤處理
- 錯誤信息
- 數據校驗
- 國際化
- 路徑常量
- REST API
- 定時任務
- 線上部署
- 推薦模塊
- API
- think
- think.base
- think.http.base
- http
- controller
- rest controller
- model
- model.mongo
- middleware