最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mongoose model, array of strings, array of objects structure - Stack Overflow

programmeradmin3浏览0评论

I am trying to design my database model and i dont have clue how to fit array of strings and array of objects into it. My current model is:

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 data structure is ['234', '564', '213', '782']

cart example data structure is:

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

How can i add this as configuration to the mongoose model?

I am trying to design my database model and i dont have clue how to fit array of strings and array of objects into it. My current model is:

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 data structure is ['234', '564', '213', '782']

cart example data structure is:

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

How can i add this as configuration to the mongoose model?

Share Improve this question edited Mar 14, 2020 at 13:02 SuleymanSah 17.9k6 gold badges37 silver badges60 bronze badges asked Mar 14, 2020 at 12:18 MeviaMevia 1,5642 gold badges20 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Favorites must be an array of String like this: favorites: [String]

For the cart array we have two main options:

  1. We can define the cart as an array of 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. Or we can declare cart as an array of 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 }
    }
  }
});

For both of them, when you create a document, it will look like this, note that mongoose added _id field in the card items.

{
    "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": "[email protected]",
    "hash": "hash...",
    "createdDate": "2020-03-14T12:40:29.969Z",
    "__v": 0,
    "id": "5e6cd0bd53feb32d50699b75"
}

If you don't want _id fields in cart array, you can add _id: false option the cart schema like this:

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

Here are some useful docs:

Arrays

Subdocuments

This can help people facing the issue withArray of Objects in Mongoose Schema:

  • To ensure error-proof handling of arrays of objects in Mongoose schemas, define each field with its appropriate type and default value.
  • Here we took a blog post with an array of ments. Define the ments array with a default empty array and specify the type for each field within the ments array.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const blogPostSchema = new Schema({
  title: { type: String, required: true },
  content: { type: String, required: true },
  ments: {
    type: [{
      user: { type: Schema.Types.ObjectId, ref: 'User' },
      text: { type: String, required: true },
      createdAt: { type: Date, default: Date.now }
    }],
    default: []
  }
});

const BlogPost = mongoose.model('BlogPost', blogPostSchema);

module.exports = BlogPost;

This setup ensures that even if the ments array is not provided during document creation, it defaults to an empty array, and each ment object follows the defined structure.

发布评论

评论列表(0)

  1. 暂无评论