javascript - 无法在 mongoDB 中创建唯一索引
问题描述
const popupSchema = new mongoose.Schema(
{
/**
* @type schema field / ObjectId
* @description User whose popus are stored.
* @note should be required
*/
user_id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required:true,
index: { unique: true }
},
/**
* @type schema field / Array
* @description Information of all the popups of the User.
*/
popupsArray: [popupDetails]
},
opts
);
这是我的架构,我想让 user_id 成为 unique_index。架构中没有其他具有相同 user_id 的条目。我正在运行此脚本以实现相同的目的:
await popups
.createIndexes(
{user_id:1}
)
.then(res => {
console.log("done")
}
).catch((err)=>{
console.log("error in indexing popups",err)
})
但是此脚本以错误终止=>
名称索引:user_id_1 已存在不同的选项。{ 好的:0,代码:85,代码名称:'IndexOptionsConflict'}
如何使 user_id 唯一索引?我也在 user_id 中尝试了 required:true 但它不起作用。
我required:true
在运行此脚本之前添加了。这会有什么不同吗?
解决方案
推荐阅读
- javascript - javascript/html - 弹出/模式中的文本区域不可编辑
- java - 如何编写一个涵盖尽可能多用例的通用 Java 函数式接口?
- apache-spark - 在 k8s 集群上运行 spark-operator
- java - Http 响应解码的行为从 Windows 到 Linux 不同
- java - 如何访问片段内的小部件,然后在主活动中使用它们?(安卓工作室)
- mediapipe - 如何获取 Mediapipe 地标索引?
- c - 尝试 malloc 结构数组,但堆缓冲区溢出
- java - C# 的 Java 代码的 SecretKeySpec AES 等效性
- node.js - 在 webpack 中使用 gzip 时出现 tapPromise 错误
- bash - bash 中有没有办法将“双分叉”脚本的标准输出连续记录到文件中?