猫鼬将属性组合为一个复合属性

Owe*_*wal 9 mongoose node.js node-modules

我正在开发一个Web应用程序。后端部分是使用Mongoose用NodeJS编写的。

我有一个简单的用户架构:

const UsersSchema = new Schema({
  login: {
    type: String,
    required: true,
    lowercase: true,
    trim: true,
    unique: true
  },
  password: {
    type: String,
    required: true,
    trim: true
  },
  email: {
    type: String,
    required: true,
    trim: true
  },
  firstName: {
    type: String,
    required: true,
    trim: true
  },
  lastName: {
    type: String,
    required: true,
    trim: true
  }
});
Run Code Online (Sandbox Code Playgroud)

我需要返回以添加该fullName属性,而无需将其实际添加到数据库中。我当然可以那样串联this.firstName + " " + this.lastName

但是我想知道是否有更好的方法来获得期望的行为。

我还使用mongoose-pagination-2库来支持项目的分页:

const users = await Users.paginate({}, queryParams);
Run Code Online (Sandbox Code Playgroud)

请给一些建议。谢谢。