【发布时间】:2013-08-24 04:21:55
【问题描述】:
我在猫鼬上设置了以下架构,我使用的是 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 } });
还有以下虚拟:
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;
});
当我进行聚合查询时:
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 ( ) ....
此时虚拟对象返回,但返回的属性为 this.cover 或 this.video 未定义。
当我做 Post.findOne(..).lean().populate(...) 等时,我根本没有得到虚拟,也没有 Post.find().lean().populate (...)
我是否遗漏了 Post 架构上的某些内容,以便能够返回虚拟对象,或者我做错了什么? 为什么使用聚合操作时,virtuals 会返回未定义的值“this.cover”?
谢谢!
【问题讨论】:
标签: mongoose