mongodb - 无法将多个文档推送到集合 (11000)
问题描述
这是我的操作模型:
const mongoose = require('mongoose');
const operationSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
extract: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Extract'
},
normalizations: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Normalize'
}],
ingestions: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Ingest'
}]
});
module.exports = mongoose.model('Operation', operationSchema);
这就是我创建操作的方式:
exports.create = async (req, res, nexr) => {
let op = new Operation
op.user = mongoose.Types.ObjectId(req.body.user);
op.extract = mongoose.Types.ObjectId(req.body.extract);
op.normalizations = [];
op.ingestions = [];
const operation = new Operation(op);
try {
await operation.save();
console.log(operation)
res.send(operation);
} catch (err) {
console.log(operation)
res.status(500).send(err);
}
}
任何用户、提取、规范化、摄取模型中都不存在唯一标签
当我尝试推送到 Mongo 时,第一次一切正常。
如果我尝试推送多个记录,我会从 Postman 收到此错误:
{
"driver": true, "name": "MongoError", "index": 0, "code": 11000, "keyPattern": { "extract.name": 1 }, "keyValue": { "extract.name": null } }
解决方案
集合中只有一个文档可以缺少唯一字段。此集合中已经有一个文档缺少值或空值。您尝试插入的集合中不存在索引。所以试试这个,从数据库中删除该集合并再次运行程序。任何缺少 source_references.key 字段的文档都将被视为具有空值。如果您希望索引仅适用于具有 source_references.key 字段的文档,您可以使用 sparse:true 索引创建选项。
推荐阅读
- php - 数据不会从 MYSQL 中的表中删除
- oop - 如何在 Rust 中以 OOP 样式“继承”属性?
- ssl - 无法在本地验证发行人的权限
- javascript - 如何两个知道通过谷歌登录的用户的两因素身份验证状态
- ansible - 在 playbook 中全局获取文件和变量
- powershell - 当有些是可选的时,Powershell 比较订单项
- c# - 即使编写为忽略异常,try-catch 块内的代码也会引发异常并中断执行
- javascript - 关闭响应式菜单“点击”JavaScript
- c++ - 虚拟地整理碎片内存,就好像它在 C++ 中是连续的一样
- jestjs - 如何测试使用 apollo 客户端 Reactive Var 的组件