如何在模型的一个参数中计算列表的长度,并将该长度添加到同一模型的另一个参数中,以便api可以将其作为响应的一部分发送回去? >
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const ArrayOfUser = require('../models/arrayofuser')
var usersSchema = new Schema({
items: {type: mongoose.Schema.Types.ObjectId, ref: 'ArrayOfUser'},
total: items.length
})
module.exports = mongoose.model('Users', usersSchema)
答案 0 :(得分:0)
这些被称为virtual properties,是猫鼬直接提供的功能:
根据您的情况实现此目标的方法是:
let g:airline_symbols.linenr = ''
let g:airline_section_z = airline#section#create(['%3p%%: ', 'linenr', '/%3L', ':%3v'])
这是下面反馈的固定节点路由方法!祝好运 :)
const usersSchema = new Schema({
items: {type: mongoose.Schema.Types.ObjectId, ref: 'ArrayOfUser'},
},{
toObject: {virtuals: true}, toJSON: {virtuals: true}
})
usersSchema.virtual('total')
.get(function() {return this.items.length})