javascript - 无法将默认值添加到数组中的对象

标签 javascript mongodb mongoose

我正在尝试将 fancyItem 添加到下面的模型中。每当我尝试添加下一个 fancyITem 时,我都会收到以下 mongo db 错误:

MongoError: E11000 duplicate key error collection: natours.fancyitems index: books.name_1 dup key: { : null }\n

为了避免这种情况,我尝试向对象添加默认值,这样我就不会收到此重复错误。

请提出更好的方法来处理这个问题!

const mongoose = require("mongoose");

const fancyItemSchema = new mongoose.Schema({
  firstName: {
    type: String,
    required: [true, "Please enter fancyItem's first name"]
  },
  lastName: {
    type: String
  },
  genre: {
    type: String,
    enum: [
      "guidingLights",
      "luminaries",
      "mavericScientists",
      "menOfLetters",
      "theGrandPhilosophers",
      "architectsOfTheFuture"
    ],
    required: true
  },
  notableWork: {
    type: String,
    required: [true, "Please enter notable work"]
  },
  quotes: [
    {
      quote: {
        type: String,
        default: "There is no quote"
      }
    }
  ],
  books: [
    {
      bookName: {
        type: String,
        sparse: true,
        default: "There is no quote"
      },
      bookURL: {
        type: String,
        sparse: true,
        default: "There is no quote"
      }
    }
  ],
  videos: [
    {
      videoName: {
        type: String,
        maxlength: [
          50,
          "A video description must have less or equal to 50 characters"
        ],
        sparse: true,
        default: "There is no quote"
      },
      videoURL: {
        type: String,
        sparse: true,
        default: "There is no quote"
      }
    }
  ],
  courses: [
    {
      courseName: {
        type: String,
        sparse: true,
        default: "There is no quote"
      },
      courseURL: {
        type: String,
        sparse: true,
        default: "There is no quote"
      },
      platform: {
        type: String,
        sparse: true,
        default: "There is no quote"
      }
    }
  ]
});

const FancyItem = mongoose.model("FancyItem", fancyItemSchema);

module.exports = FancyItem;

最佳答案

您提供的错误表明已经有一条名称为 null 的记录。换句话说,您已经拥有一本没有名字的书。

相关文档:

If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.

You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.

Unique Indexes

Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.

Sparse Indexes

关于javascript - 无法将默认值添加到数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59508926/

相关文章:

sorting - 在 Mongoose 聚合框架中按日期排序

node.js - 使用 mongoose 验证整数值

javascript - 使用 jquery 查找和替换 html 中的元素

javascript - 尝试在价格计算器上显示选定的服务

javascript - 不断获取 HTTP REQUEST 的 HTTP 响应代码 "1"

node.js - NodeJS 带有 byId() 的额外字段;

javascript - 如何在 Mongoose 中为同一字段返回多个计数?

javascript - Mongoose MongoNetworkError//连接错误

javascript - 网络应用程序设计思想

javascript - 如何将CSS应用于动态HTML内容?