首页 > 解决方案 > 如果不为空,则为唯一的猫鼬模式

问题描述

我有一个包含 2 种不需要的类别的模式,我想创建 2 个 expCat 但它让我出错,因为第一个 colCat 变为空,因此第二个也变为空,这让我因为唯一而出错。

const categoryModel = mongoose.Schema({
  colCat: { type: String, unique: true, sparse: true, index: true },
  expCat: { type: String, unique: true, sparse: true, index: true },
});

标签: javascriptmongodbmongoose

解决方案


推荐阅读