The think.model.mongo
class inherit from think.model.base。
export default class extends think.model.mongo {
getList(){
}
}
module.exports = think.model('mongo', {
getList: function(){
}
})
Set indexes of field, before operate data it will set index automatically.
export default class extends think.model.mongo {
init(...args){
super.init(...args);
// set indexes
this.indexes = {
}
}
}
export default class extends think.model.mongo {
init(...args){
super.init(...args);
// set index
this.indexes = {
name: 1
}
}
}
With $unique
to set unique index, like:
export default class extends think.model.mongo {
init(...args){
super.init(...args);
// set index
this.indexes = {
name: {$unique: 1}
}
}
}
Multi-field index, like:
export default class extends think.model.mongo {
init(...args){
super.init(...args);
// set index
this.indexes = {
email: 1
test: {
name: 1,
title: 1,
$unique: 1
}
}
}
}
Primary key name, default is _id
, get it with this.getPk
.
Where condition in mongo model is different from relational database.
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();
}
}
More details in https://docs.mongodb.org/manual/reference/operator/query/#query-selectors。
return
{Promise}Get handler which operate current table.
export default class extends think.model.mongo {
async getIndexes(){
let collection = await this.collection();
return collection.indexes();
}
}
Aggregate query, more details in https://docs.mongodb.org/manual/core/aggregation-introduction/。
mapReduce operate, more details in https://docs.mongodb.org/manual/core/map-reduce/。
indexes
{Object} index optionsoptions
{Object}Create indexes.
return
{Promise}Get indexes.