如何访问聚合中的方法?
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