mongoose lean query, virtuals not showing

后端 未结 2 1661
遇见更好的自我
遇见更好的自我 2021-01-04 20:09

I have the following schema set up on mongoose, Im using version 3.6.17:

var PostSchema = new Schema({
    _id: { type: String, required: true, index: { uniq         


        
相关标签:
2条回答
  • 2021-01-04 20:50

    i found a solution you can use mongoose-lean-virtuals plugin

    https://www.npmjs.com/package/mongoose-lean-virtuals

    0 讨论(0)
  • 2021-01-04 20:53

    lean queries return raw MongoDB driver response as a plain js object. So, there are no getters, setters, virtuals or other "Mongoose magic" in it. See Api docs for more info.

    The point of lean queries is to return your objects as fast as possible. If you need virtuals - use ordinary Mongoose queries.

    As for aggregation, it's 100% MongoDB feature and Mongoose can't control it. So, when you calling aggregate from Mongoose it works the same as aggregate in MongoDB console. aggregate can't operate with virtuals, because there are no such fields in your database. Mongoose can't even cast your aggregation query according to your schema (like it's doing with findOneAndUpdate arguments), because aggregation changes the shape of the document on each step. See Mongoose API Docs and MongoDB Docs for more info.

    0 讨论(0)
提交回复
热议问题