【问题标题】:mongoose model, array of strings, array of objects structure猫鼬模型,字符串数组,对象数组结构
【发布时间】:2020-03-14 21:22:03
【问题描述】:

我正在尝试设计我的数据库模型,但我不知道如何将 arraystringsarrayobjects 放入其中。我目前的模型是:

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

const schema = new Schema({
    email: { type: String, unique: true, required: true },
    hash: { type: String, required: true },
    createdDate: { type: Date, default: Date.now },
    settings: {
        favorites: { /* ??? */ },
        cart: { /* ??? */ },
        states: {
            favorites: { type: Boolean, default: true },
            search: { type: Boolean, default: false },
            category: { type: Schema.Types.Mixed, default: false }
        }
    }
});

schema.set("toJSON", { virtuals: true });

module.exports = mongoose.model("User", schema);

favorites数据结构为['234', '564', '213', '782']

cart示例数据结构为:

[
    { quantity: 5, marketId: '234' },
    { quantity: 2, marketId: '564' },
    { quantity: 7, marketId: '213' },
    { quantity: 3, marketId: '782' }
]

如何将此作为配置添加到mongoose 模型?

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    Favorites 必须是这样的字符串数组:favorites: [String]

    对于购物车数组,我们有两个主要选项:

    1. 我们可以将购物车定义为subdocuments 的数组。
    const schema = new Schema({
      email: { type: String, unique: true, required: true },
      hash: { type: String, required: true },
      createdDate: { type: Date, default: Date.now },
      settings: {
        favorites: [String],
        cart: [
          {
            quantity: Number,
            marketId: String
          }
        ],
        states: {
          favorites: { type: Boolean, default: true },
          search: { type: Boolean, default: false },
          category: { type: Schema.Types.Mixed, default: false }
        }
      }
    });
    
    1. 或者我们可以将购物车声明为schema types 的数组。
    const schema = new Schema({
      email: { type: String, unique: true, required: true },
      hash: { type: String, required: true },
      createdDate: { type: Date, default: Date.now },
      settings: {
        favorites: [String],
        cart: [
          new Schema({
            quantity: Number,
            marketId: String
          })
        ],
        states: {
          favorites: { type: Boolean, default: true },
          search: { type: Boolean, default: false },
          category: { type: Schema.Types.Mixed, default: false }
        }
      }
    });
    

    对于他们两个,当你创建一个文档时,它会是这样的,注意mongoose在卡片项目中添加了_id字段。

    {
        "settings": {
            "states": {
                "favorites": true,
                "search": false,
                "category": false
            },
            "favorites": [
                "234",
                "564",
                "213",
                "782"
            ],
            "cart": [
                {
                    "_id": "5e6cd0bd53feb32d50699b79",
                    "quantity": 5,
                    "marketId": "234"
                },
                {
                    "_id": "5e6cd0bd53feb32d50699b78",
                    "quantity": 2,
                    "marketId": "564"
                },
                {
                    "_id": "5e6cd0bd53feb32d50699b77",
                    "quantity": 7,
                    "marketId": "213"
                },
                {
                    "_id": "5e6cd0bd53feb32d50699b76",
                    "quantity": 3,
                    "marketId": "782"
                }
            ]
        },
        "_id": "5e6cd0bd53feb32d50699b75",
        "email": "abc@def.net",
        "hash": "hash...",
        "createdDate": "2020-03-14T12:40:29.969Z",
        "__v": 0,
        "id": "5e6cd0bd53feb32d50699b75"
    }
    

    如果您不想在购物车数组中添加 _id 字段,您可以在购物车架构中添加 _id: false 选项,如下所示:

        cart: [
          new Schema(
            {
              quantity: Number,
              marketId: String
            },
            { _id: false }
          )
        ],
    

    这里有一些有用的文档:

    Arrays

    Subdocuments

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 2018-02-21
      • 2016-03-09
      • 2021-01-24
      • 2015-12-28
      • 2021-05-06
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多