node.js - 如何在 Mongoose 模式中使用项目的值

标签 node.js mongodb mongoose

我想获取架构中项目的值并在同一架构中使用它。

例如,有一个

const mongoose = require("mongoose");
const TestSchema = new mongoose.Schema({
    appellant: String,
    respondent: String,
    title: `${this.appellant} V. ${this.respondent}`,
});

module.exports = mongoose.model("Test", TestSchema);

最佳答案

通常,virtual schema用于此类情况。它允许您以特定格式使用数据,而无需保留数据并创建冗余数据。

就您而言,它看起来像这样:

TestSchema.virtual('title').get(function () {
   return this.appellant + ' V. ' + this.respondent
});

关于node.js - 如何在 Mongoose 模式中使用项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57395889/

相关文章:

node.js - 无法使用新 Node js 8.10 将数据插入 DynamoDB

java - 使用Java ProcessBuilder调用全局安装的 Node 模块

Node.js 生产设置

java - 在 MongoDB Java 驱动程序中聚合时解析数据类型

c# - 使用node对mongodb进行多次查询

node.js - 更新命令 mongoskin 后的对象 ID

MySQL 在一段时间后断开连接

javascript - MongoDB 脚本和 CSV 文件 : can I use require. js 加载 jquery.csv?

node.js - 是否可以在集合级别设置读取首选项?

javascript - 如何在 Mongoose 中动态删除所需的验证