node.js - 如何在 MongoDB 中保存相同的文档?
问题描述
我的架构
const mongoose = require('mongoose');
const test_schema = new mongoose.Schema({
Name: {
type: String
}
});
const chatting = mongoose.model('chat', test_schema);
module.exports = chatting;
获取上述模型给出架构
const chat = require('./models/chatting.js');
保存变量
const one = new chat({
Name : "John"
})
const two = new chat({
Name : "John"
})
现在保存
await chat.insertMany([one, two])
但我得到重复的名称键错误
解决方案
您向我们提供了错误的架构。(test_schema != chatting_schema)。检查您的聊天模式,看看它的结构是否有点像这样:
const chatting_schema = new mongoose.Schema({
Name: {
type: String
unique: true
}
});
如果是这样,请删除唯一属性。
此外,当您已经创建了一个文档时,您可以使用 .save() 函数,例如
await one.save();
推荐阅读
- oracle - oracle将存储过程的字符串参数转换为数字数组
- mysql - SQL 从另一个表中选择值逗号分隔,其中 id 包含在字符串中
- javascript - 使用 e.stopPropagation() 时复选框更改事件冒泡到父 li 元素
- javascript - 如何删除文本框中的重复值
- ansible - Windows 上的 Ansible:如何更改注册表所有权
- python-3.x - 每晚重采样 DataFrame
- java - 在 Android 中取消缩短 URL
- android - ImageView 中图像背后的黑色背景
- codenameone - 连接请求依赖和顺序
- python - 计算熊猫数据帧中两个日期时间戳 [yyyy-mm-dd hh:mm:ss.000] 列之间的差异(以秒为单位)时出错