我想获取架构中项目的值并在同一架构中使用它。
例如,有一个
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/