javascript - 如何在 NodeJS 中的任何 mongoose 模型中使用数组?

标签 javascript arrays mongoose


    const writingSchema = {
      title: String,
      content: String
    };
    
    const userSchema = new mongoose.Schema ({
      email: String,
      password: String,
      userID: String,
      userName: String,
      yourWriting: new Array(writingSchema)
    });

这是在 Node JS 中声明对象内部数组的正确格式吗

最佳答案

我们可以声明一个 subdocuments 的数组在 Mongoose 中,例如:

const userSchema = new mongoose.Schema({
  email: String,
  password: String,
  userID: String,
  userName: String,
  yourWriting: [writingSchema]
});

关于javascript - 如何在 NodeJS 中的任何 mongoose 模型中使用数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62681007/

相关文章:

javascript - Typescript cordova 接口(interface)抛出引用错误 "ReferenceError: Cordova is not defined"

c++ - 内存位置作为放大动态数组中的最后一个值

python - 反转大型 JSON 字典

node.js - MongoError : pool is draining, 在集成测试中使用MongoMemoryServer时禁止新操作

node.js - 如何在Sinon中使用多个参数来 stub Mongoose 方法?

node.js - 跳过 Mongoose 中某些更新的时间戳中间件

javascript - 在 Google Analytics 中跟踪所有出站链接

javascript - 如何让 div 在 bar 已满时可见 [JS]

c - C 中的 Tic tac Toe 游戏在为对手找到随机数时陷入循环

javascript - 如果在 JavaScript 中的对象数组中找到对象的属性,则返回该对象的属性