我在使用虚拟机时遇到了一个非常奇怪的问题。从下面的代码示例中可以看出,当我使用find时,为了使虚拟机出现在结果中,我还必须包括从中创建虚拟机的基本列。除此之外,还有其他选择吗?否则,每次使用find时,我都必须同时选择基本库和虚拟库吗?
PersonSchema = new Schema({
fname: {type: String,required: true},lname: String,bday: {type: Date,share: mongoose.Decimal128,},{toObject:{virtuals:true},toJSON:{virtuals:true}});
PersonSchema.virtual('desc').get(function(){
const birth = this.bday.getMonth() + this.bday.getFullYear();
return this.fname + ' ' + this.lname + ' ' + this.share + '% ' ;
});
PersonSchema.virtual('Share').get(function(){ return this.share.toString});
const selectList = 'fname lname share bday desc Share';
PersonSchema.methods.getData = function() {
Persons.find({},selectList,function(err,persons){
const data = JSON.parse(JSON.stringify(persons,{virtuals: true}));
console.log('persons',persons);
}).limit(5);
}