小编art*_*nov的帖子

Mongoose:只查找并选择虚拟中指定的字段

我有一个UserSchemamongoose架构:

var UserSchema = new Schema({
  name: String,
  username: String,
  email: String,
  role: {type: String, default: 'user'},
  following: [{type: Schema.ObjectId, ref: 'User'}],
  followers: [{type: Schema.ObjectId, ref: 'User'}],
  hashedPassword: String,
  provider: String,
  salt: String,
  facebook: {},
  twitter: {},
  github: {},
  google: {}
});
Run Code Online (Sandbox Code Playgroud)

我创建了一个虚拟profile,它只返回公共配置文件的信息:

UserSchema
  .virtual('profile')
  .get(function() {
    return {
      'username': this.username,
      'name': this.name,
      'role': this.role
    };
  });
Run Code Online (Sandbox Code Playgroud)

我的问题是如何在我提出find请求时只获取此信息.这是我的代码:

UserSchema.statics = {
  list: function (options, callback) {
    var criteria = options.criteria || {};

    this.find(criteria)
      .select(/* …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js

5
推荐指数
1
解决办法
1779
查看次数

标签 统计

mongoose ×1

node.js ×1