mongoose lean query and aggregate, virtuals not showing

3,076 views
Skip to first unread message

Mauricio Mercado

unread,
Aug 22, 2013, 10:09:26 AM8/22/13
to mongoo...@googlegroups.com
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!

Aaron Heckmann

unread,
Sep 4, 2013, 6:23:22 PM9/4/13
to mongoo...@googlegroups.com
Model.aggregate returns plain objects (without virtuals):

The documents returned are plain javascript objects, not mongoose documents cast to this models schema definition (since any shape of document can be returned).



Same for lean() queries, they return plain documents, not mongoose documents.

Mongoose documents have virtuals applied, plain js objects do not.

Also, it doesn't make sense for virtuals to "return" since they do not store any data in the db, they are simply accessors that live in your application.





--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Reply all
Reply to author
Forward
0 new messages