如何在Mongoose中填充虚拟内的对象
我已经设置了一个虚拟查询,如下所示:
UserSchema .virtual('user_info') .get(function () { return { '_id': this._id, 'username': this.username, 'admin': this.admin, 'email': this.email, 'company': this.company, 'image': this.image, 'profile': this.profile }; });
company
领域具有与用户连接的参考公司的标识。 我怎样才能使它填充这个领域,以便我可以得到标题和其他领域Company
模型包含?
一个virtual
同步返回,所以你没有时间去执行一个查询,并等待getter的结果。 你可以做的是填充一个常规的对象 ,在这种情况下,你的getter的返回值:
Company.populate( someUser.user_info, // This is a plain object, not a Document. { path: "company", model: "company" // If you specify the model here, // it doesn't technically matter what // model you use on line one... }, function(err, user_info) { console.log("User info with populated company", user_info); console.log("Company name", user_info.company.name); } );
- 使用PUT和update()mongoose增加JSON字段
- mongoose – 是否有可能在运行时dynamic添加方法进行build模?
- 返回一个mongoose模型函数的结果
- Promise findOneAsync variable = {“isFulfilled”:false,“isRejected”:false}?
- 我不能将数组推入mongoose模式
- 在m sort中按datesorting超过9个logging抛出错误
- 连接使用mongoose的MongoDB的TImeout
- 如何正确创build像trello,Mongoose和Angularjs的卡片显示
- 重新格式化date正则expression式