mongoose精益查询,虚拟未显示

mau*_*ado 9 mongoose

我在mongoose上设置了以下架构,我使用版本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 } });
Run Code Online (Sandbox Code Playgroud)

以下虚拟内容:

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;
});
Run Code Online (Sandbox Code Playgroud)

当我进行聚合查询时:

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 ( ) ....
Run Code Online (Sandbox Code Playgroud)

此时虚拟返回,但它们返回属性this.cover或this.video undefined.

当我做一个Post.findOne(..).lean().populate(...)等,我根本没有得到虚拟,也没有Post.find().lean().populate(.. .)

我在Post模式上遗漏了什么,能够返回虚拟内容,还是我做错了什么?为什么使用聚合操作,虚拟会将值"this.cover"返回为undefined?

谢谢!

Leo*_*tny 22

lean查询将原始MongoDB驱动程序响应作为普通js对象返回.那么,有没有getters,setters,virtuals或其他"猫鼬魔法"在里面.有关详细信息,请参阅Api文档.

lean查询是尽可能快地恢复你的对象.如果您需要virtuals- 使用普通的Mongoose查询.

至于聚合,它是100%的MongoDB功能,而Mongoose无法控制它.所以,当你调用aggregate从猫鼬它的工作方式相同aggregateMongoDB控制台.aggregate无法与虚拟机一起运行,因为数据库中没有此类字段.Mongoose甚至无法根据您的模式转换聚合查询(就像它使用findOneAndUpdate参数一样),因为聚合会在每一步上更改文档的形状.有关详细信息,请参阅Mongoose API DocsMongoDB Docs.