如何在查找结果中使用猫鼬虚拟机

我在使用虚拟机时遇到了一个非常奇怪的问题。从下面的代码示例中可以看出,当我使用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);
}
jacksonlijie 回答:如何在查找结果中使用猫鼬虚拟机

我看到您正在尝试在选项中设置虚拟屏幕的显示,您是否尝试过使用set方法?

PersonSchema.set('toJSON',{ virtuals: true });
本文链接:https://www.f2er.com/3161242.html

大家都在问