think.model.mongo
繼承類 think.model.base。
export default class extends think.model.mongo {
getList(){
}
}
module.exports = think.model("mongo", {
getList: function(){
}
})
設(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
}
}
}
}
主鍵名,默認(rèn)為 _id
,可以通過 this.getPk
方法獲取。
mongo 模型中的 where 條件設(shè)置和關(guān)系數(shù)據(jù)庫中不太一樣。
export default class extends think.model.mongo {
where1(){
return this.where({ type: "snacks" }).select();
}
}
export default class extends think.model.mongo {
where1(){
return this.where({ type: "food", price: { $lt: 9.95 } }).select();
}
}
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();
}
}
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。
return
{Promise}獲取操作當(dāng)前表的句柄。
export default class extends think.model.mongo {
async getIndexes(){
let collection = await this.collection();
return collection.indexes();
}
}
聚合查詢。具體請見 https://docs.mongodb.org/manual/core/aggregation-introduction/。
mapReduce 操作,具體請見 https://docs.mongodb.org/manual/core/map-reduce/。
indexes
{Object} 索引配置options
{Object}創(chuàng)建索引。
return
{Promise}獲取索引。
文檔地址:https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_model_mongo.md
更多建議: