聚合中的mongoose模式方法

时间:2017-08-03 15:06:43

标签: node.js mongodb mongoose schema aggregate

如何访问聚合中的方法?

let
    mongoose = require("mongoose"),
    Schema = mongoose.Schema;

let
    UserSchema = new Schema({
        name: String,
        lastname: String
    });


UserSchema.methods.fullname = () => {
    return this.name + " " + this.lastname;
};

let
    UserModel = mongoose.model("user",UserSchema);


let 
    query = [
        {
            $match: {
                "name": /foo/i
            }
        }
    ];

UserModel.aggregate(query, (err, users) => {
    if(err) throw new Error(err);

    for(let user of users){
        console.log(user.fullname());
    }

});

和结果

TypeError: user.fullname is not a function

我可以用这个

UserModel.findOne(query, (err, user) => {
    if(err) throw new Error(err);

    console.log(user.fullname());

});

结果

foo bar

但是我需要为我的真实查询聚合得到其他参数($ addFields,$ lookup,$ filters和其他)

thx = D

0 个答案:

没有答案