我试图让MongoDB根据其索引检测重复值.我认为这在MongoDB中是可能的,但是通过Mongoose包装器似乎已经破坏了.对于这样的事情:
User = new Schema ({
email: {type: String, index: {unique: true, dropDups: true}}
})
Run Code Online (Sandbox Code Playgroud)
我可以使用相同的电子邮件保存2个用户.该死.
这里也表达了同样的问题:https://github.com/LearnBoost/mongoose/issues/56,但那个线程已经老了,无处可去.
现在,我手动调用数据库来查找用户.由于"电子邮件"已编入索引,因此该电话费用并不昂贵.但让它本地处理仍然是件好事.
有人有解决方案吗?
线程"main"中的异常com.mongodb.MongoException $ DuplicateKey:{"serverUsed":"localhost/127.0.0.1:27017","err":"E11000重复键错误索引:twitterdb03.LevelAFollowers.$ id dup key:{ :ObjectId('52d5636de408652b4853a8fe')}","code":11000,"n":0,"connectionId":12,"ok":1.0}
我正在使用mongo 2.11.1
在java中从未遇到过简单的写操作问题
myMap.put(inid, followersList);
myObj.putAll(myMap);
myIdMapCollection.insert(myObj);
Run Code Online (Sandbox Code Playgroud) 我已经为新项目创建了一个新的 Mongo 集合,但是当我尝试创建新用户时,出现以下错误:
\nMongoError: E11000 duplicate key error collection: GestorMedico.users index: username_1 dup key: { username: null }\nRun Code Online (Sandbox Code Playgroud)\n我的用户架构如下:
\nconst { Schema, model } = require("mongoose");\n\nconst userSchema = new Schema({\n nombreEmpresa: {\n type: String,\n unique: true,\n required: true\n },\n email: {\n type: String,\n required: true\n },\n telefono: {\n type: String,\n required: true\n },\n contrase\xc3\xb1a: {\n type: String,\n required: true\n }\n}, {\n timestamps: true,\n versionKey: false\n});\n\nmodule.exports = model("Users", userSchema);\nRun Code Online (Sandbox Code Playgroud)\n我的功能如下:
\nuserCtrl.createUser = async (req, res) => …Run Code Online (Sandbox Code Playgroud) 我读过,当我使用 pymongo 的 upsert 且不提供“_id”时,upsert 将尝试生成新的 Id,这将导致操作失败。这是真的?以及如何在不使用“_id”的情况下更新插入?
这是我使用 pymongo 的replace_one:
db['dataitemdetails'].replace_one({'asset_Id':tdata['asset_id'],'period_type':tdata['period_type'],'detail_id':tdata['detail_id'], 'currencycode':tdata['currencycode'],'dataitem_Id':tdata['dataitem_id'],'period_end':tdata['period_end'], 'scenario_id':tdata['scenario_id'],}, tdata, upsert=True)
Run Code Online (Sandbox Code Playgroud)
我创建了一个复合索引并使用我的搜索条件设置为唯一,这样在集合中我搜索的内容将始终是唯一的(如果存在)。
并使用replace_one我收到此错误:
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: finance.dataitemdetails index: asset_id_1_dataitem_id_1_detail_id_1_period_type_1_scenario_id_1_currencycode_1_period_end_1 dup key: { : 19, : 1211, : 0, : "Month", : 1, : "RC", : new Date(949276800000) }
Run Code Online (Sandbox Code Playgroud)
这是我使用相同过滤器查找的查询,它返回 1 个文档。
> db.dataitemdetails.find({'asset_id':19,'dataitem_id':1211,'detail_id':0,'period_type':'Month','currencycode':'RC','period_end':new Date(949276800000)})
{ "_id" : ObjectId("5c7721c17314e53a85be7e89"), "Value" : "USD", "period_end" : ISODate("2000-01-31T00:00:00Z"), "currencycode" : "RC", "scenario_id" : 1, "dataitem_id" : 1211, "period_type" : "Month", "detail_id" : 0, "asset_id" : 19 } …Run Code Online (Sandbox Code Playgroud) 我想使该集合中的唯一电子邮件变得唯一,但是我无法正常工作,这是我的服务器代码。
// Create a schema
var userSchema = new mongoose.Schema({
email: { type: String, required: true},
password: String
});
var userModel = mongoose.model("user", userSchema);
router.post('/postuser', (req, res) => {
console.log('Requested data to server: ' + JSON.stringify(req.body._user));
var user = new userModel({
email: req.body._user.email,
password: req.body._user.password
});
// user.isNew = false;
user.save((err, data) => {
console.log('Analyzing Data...');
if(data) {
console.log('Your data has been successfully saved.');
res.json(data);
}
else {
console.log('Something went wrong while saving data.');
console.log(err);
res.send(err);
}
})
});
Run Code Online (Sandbox Code Playgroud)
注意:我也尝试了, …
每当我尝试注册用户时,它都会给我这个错误
我检查了 db 集合并且不存在这样的重复条目,让我知道我做错了什么?
仅供参考 - req.body.email 和 req.body.password 正在获取值。
我也检查了这篇文章,但没有帮助堆栈链接
如果我完全删除,那么它会插入文档,否则即使我在 local.email 中有一个条目,它也会抛出错误“重复”错误
Server started on port 5000
MongoDB Connected
MongoError: E11000 duplicate key error collection: test.users index: email1_1 dup key: { email1: null }
{ driver: true,
name: 'MongoError',
index: 0,
code: 11000,
keyPattern: { email1: 1 },
keyValue: { email1: null },
errmsg: 'E11000 duplicate key error collection: test.users index: email1_1 dup key: { email1: null }',
[Symbol(mongoErrorContextSymbol)]: {}
}
Run Code Online (Sandbox Code Playgroud)
以下是我在 user.js 模型架构中的用户架构
const mongoose = require('mongoose'); …Run Code Online (Sandbox Code Playgroud) mongodb ×6
node.js ×4
mongoose ×3
javascript ×2
java ×1
nodemailer ×1
pymongo ×1
python ×1
twitter4j ×1