【问题标题】:Is it possible to push a Mongoose obj schema into an array?是否可以将 Mongoose obj 模式推送到数组中?
【发布时间】:2021-08-25 16:35:09
【问题描述】:

我已将我的 express 应用程序连接到 MongoDB Atlas 集群。这就是我所拥有的,它目前正在向它发布数据:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const applicantSchema = new Schema(
    {
        _name: {
            type: String,
            required: true,
        },
        _a1: {
            type: String,
            required: true,
        },
        ...etc
    },
    { timestamps: true }
);

const Applicant = mongoose.model("Applicant", applicantSchema);

module.exports = Applicant;

当数据记录到控制台时,我看到:

{
  _name: 'fsd',
  _a1: 'x',
  _a2: 'y',
  _a3: 'z'
}

问题:是否可以将架构的结果显示为数组,例如:

[{
  _name: 'fsd',
  _a1: 'x',
  _a2: 'y',
  _a3: 'z'
}]

【问题讨论】:

  • 你的问题(至少对我来说)很不清楚。你能详细说明你想做什么吗?
  • @eol 我想知道是否可以将applicantSchema 的结果显示为一个数组,或者它是否必须是一个对象

标签: node.js arrays mongodb express mongoose


【解决方案1】:

是的,这是可能的。您正在寻找的是一对多的关系。
你可以使用check mongoose docs here.

另外,您可以找到例如here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多