I have the following schema set up on mongoose, Im using vesrion 3.6.17:
var PostSchema = new Schema({
_id: { type: String, required: true, index: { unique: true } },
video: { type: String, default: ''},
cover: { type: String, default: ''},
createdAt: { type: Date, default: Date.now },
lastUpdate: { type: Date, default: Date.now }
}, { autoIndex: true, toObject: { virtuals: true }, toJSON: { virtuals: true } });
And the following virtuals:
PostSchema.virtual('replied').get(function () {
return false;
});
PostSchema.virtual('cover_url').get(function () {
return config.cover.server + this.cover;
});
PostSchema.virtual('video_url').get(function () {
return config.video.server + this.video;
});
When I do an aggregate query:
Post.aggregate( { $match: { replyTo: { $ne: "" }, author: user._id, draft: false } },
{ $project: {
_id: 1,
video: 1,
video_url: 1,
cover: 1,
cover_url: 1,
createdAt: 1,
lastUpdate: 1,
Ireplied : { $not: "$replied"} }
}, function ( ) ....
At this point the virtuals return but they return with the attribute this.cover or this.video undefined.
And when I do a Post.findOne(..).lean().populate(...) etc, I dont get the virtuals at all, neither with a Post.find().lean().populate(...)
Am I missing something on the Post schema, to be able to return the virtuals, or am I doing something wrong?
And why with the aggregate operation the virtuals return the value "this.cover" as undefined?
Thank you!