W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
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
}
}
}
通過(guò) $unique
來(lái)指定為唯一索引,如:
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
,可以通過(guò) this.getPk
方法獲取。
mongo 模型中的 where 條件設(shè)置和關(guān)系數(shù)據(jù)庫(kù)中不太一樣。
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();
}
}
更多文檔請(qǐng)見(jiàn) 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();
}
}
聚合查詢。具體請(qǐng)見(jiàn) https://docs.mongodb.org/manual/core/aggregation-introduction/。
mapReduce 操作,具體請(qǐng)見(jiàn) 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
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: