Mongo在Meteor应用程序的_id_字段上给出重复的密钥错误 [英] Mongo gives duplicate key error on _id_ field in Meteor application

查看:86
本文介绍了Mongo在Meteor应用程序的_id_字段上给出重复的密钥错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索很长一段时间,并且有很多有关此问题的主题.到目前为止,我找不到任何解决方案.而且,对我来说还不是很清楚,希望您能提供帮助.这是我的问题:

I'm searching a long time and a lot of topics about this issue. Until now I couldn't find any solution. Moreover, it's not all clear to me, hopefully you can help. Here's my question:

我设计了一个Meteor应用程序,Mongo DB中有一个带有订单的集合.该集合可以通过读取一个csv文件来填充

I designed a Meteor application and there is a collection in the Mongo DB with orders. That collection is filled by reading a csv file

import_file_orders = function(file) {
var lines = file.split('%\r\n');
var l = lines.length - 1;
for (var i=0; i < l; i++) {
  var line = lines[i];
  var line_parts = line.split('|');
  var ex_key = line_parts[0];
  var ex_name = line_parts[1];
  var clin_info = line_parts[2];
  var order_info = line_parts[3];
  var clinician_last_name = line_parts[4];
  var clinician_first_name = line_parts[5];
  var clinician_code = line_parts[6];
  var clinician_riziv = line_parts[7]
  var pat_id = line_parts[8];
  Meteor.orders.insert({Patient:pat_id, Exam_code:ex_key, Exam_name:ex_name, Clinical_info:clin_info, Order_info:order_info, Clinician:{first:clinician_first_name, last:clinician_last_name, c_code:clinician_code, riziv:clinician_riziv}, Planned:null});
  console.log("%");
};
}

读取CSV文件后,集合中的某些文档有错误:

After reading the CSV file there are errors for some documents in the collection:

duplicate key error index: protocolplanner.Orders.$_id_ dup key: { :     "2ZGvRfuD8iMvRiXJd" } insert failed

当我运行Mongo命令db.Orders.getIndexes()时,我看到有两个索引:

When I run the Mongo command db.Orders.getIndexes() I see that there are two indexes:

{
  "v" : 1,
  "key" : {
           "_id" : 1
   },
   "name" : "_id_",
   "ns" : "protocolplanner.Orders"
 }

似乎有两个索引:一个_id索引(始终存在并且不能删除)和一个_ id _索引.似乎_ id _索引导致了此错误.所以我有三个问题:

It seems that there are two indexes: one _id index (which is always there and can't be deleted) and one _ id _ index. It seems that the _ id _ index caused the error. So I have three questions:

首先:为什么会有_ id _索引?我在Meteor代码中未定义任何索引. 第二:为什么该索引存在重复键错误? 第三:同样看来,我无法删除_ id _索引.为什么?现在,您不能删除_id索引,但我认为这不是_id索引.

First: Why is there an _ id _ index? I defined no indexes in my Meteor code. Second: Why is there a dup key error for that index? Third: Also it seems that I can't remove the _ id _ index. Why is it? I now that you can't remove a _id index but in my opinion this isn't an _id index.

您可以看到我完全迷失了.请帮忙!

As you can see I'm totally lost. Please help!

如下所述,更多信息:

我正在读取的数据量为10151行.在定义的客户端读取文件的功能.通过允许和拒绝规则,只有管理员用户才能将数据输入mongo.正确读取行.读取文件后,该应用程序中的所有数据均可用.几秒钟后,Mongo会自动创建索引,并出现错误.从那时起,在应用程序中将不再显示发生错误的行.

The amount of data I'm reading is 10151 lines. The function that reads the file in defined client side. Via allow and deny rules only admin users can enter the data into mongo. The lines are read correctly. After reading the file all data is available in the app. After a few seconds the index is automatically created by Mongo and the error appear. From then the lines for which the error occurs aren't visible anymore in the app.

我在Mongo shell中尝试了以下操作: db.Orders.find({_ id:"2ZGvRfuD8iMvRiXJd"})

I tried the following in the Mongo shell: db.Orders.find({_id:"2ZGvRfuD8iMvRiXJd"})

Mongo给了我正确的文档.这证明_id确实是在将数据插入DB中时由Meteor创建的.但是,此_id应该是唯一的,因此我对自己的错误完全感到困惑.

Mongo gives me the right document. This proves that the _id indeed is created by Meteor when the data is inserted in the DB. However this _id should be unique so I'm totally confused about the error I have.

经过反复试验后,我得到了有关此问题的一些新信息.也许这很有趣,所以我们可以找到这个问题的答案.

EDIT 2: After some trial and error I have some new information about this problem. Maybe It's interesting to know, so we can find an answer on this problem.

像上面描述的那样,当我在客户端读取数据时,即使使用ObjecID而不是Meteor ID,我也会遇到重复的密钥错误.但是,当我通过mongoinsert命令将数据直接推入Mongo时,所有数据均已正确导入,并且不会发生错误.插入此数量的数据时,服务器和客户端之间似乎有冲突(可能是异步计时问题).

Like described above, when I read the data on client side, I have the duplicate key error, even when I use ObjecID instead of Meteor ID. However when I push the data directly into Mongo via mongoinsert command, all the data is imported well and no error occurs. It seems that there's a conflict between server and client when I insert this amount of data (maybe asynchronous timing issues).

此刻,我正在寻找一种解决方案,以读取数据服务器端,希望不会发生任何错误.

At this moment I'm searching a solution to read the data server side in the hope no error occurs.

推荐答案

这不会解决您的问题,但是它应该为您指明正确的方向,并且也许可以使您隔离出可用于创建新问题的问题问题:

This won't solve your problem, but it should point you in the right direction and maybe enable you to isolate the problem that you can use to create a new question:

第一:为什么会有_id_索引?

没有.只有一个索引,它有一个名称和一个密钥描述符.那不是同一回事.默认索引的名称_id_,其键为_id.

There isn't. There's only one index, and it has a name and a key descriptor. That's not the same thing. The name of the default index is _id_, its key is _id.

为什么该索引存在重复键错误?

Why is there a dup key error for that index?

_id通常是在客户端而非服务器端创建的.问题在于这些键来自何处,因为2ZGvRfuD8iMvRiXJd当然不是ObjectId.这可能是一个流星键,或者您使用了一些自定义的主键,但是我不知道这些键是如何生成的.也许生成密钥的任何内容都容易发生冲突?

The _id is normally created client-side, not server-side. The question is where those keys come from, because 2ZGvRfuD8iMvRiXJd is certainly not an ObjectId. This might be a meteor key, or you use some custom primary key, but I don't know how these keys are generated. Maybe whatever generates the key is susceptible to collisions?

有关此问题的更多信息会有所帮助,但我建议改写一个新问题,以使问题不会变得太大或没有太多历史.

More info on that would be helpful, but I'd suggest phrasing a new question so the question doesn't grow too large or get a lot of history.

第三:同样,我似乎也无法删除_ id _索引.为什么呢?

Third: Also it seems that I can't remove the _ id _ index. Why is it?

这是第一个答案的引理:您不能删除强制性主键索引.

That's a lemma of the first answer: You can't delete the mandatory primary key index.

流星默认情况下生成ID的方式与MongoDb 不同.这是有道理的,因为 ObjectId的约定使得如果客户端数量很大(例如,如果客户端不是服务器实例,而是客户端浏览器,其中可能还有2-3个数量级.

Meteor, by default, generates ids in a different way than MongoDb. That makes sense, because the convention for ObjectId makes collisions probable if the number of clients is large (i.e. if the clients aren't server instances, but client browsers, of which there are probably 2-3 orders of magnitude more).

相反,Meteor显然使用方法来始终生成伪客户端和服务器上的随机数.令人烦恼的是,该实现使用PRNG,并且退回为非加密货币-确定性随机数发生器(Alea).换句话说,找出ID是如何精确生成的可能很棘手,因为它取决于环境的许多细节.

Instead, Meteor apparently uses method to consistently generate pseudo-random numbers on client and server. Irritatingly, the implementation uses a PRNG and falls back to a not crypto-strong deterministic random number generator (Alea). In other words, finding out how exactly your ids are being generated could be tricky because it depends on a lot of details of your environment.

解决方法: 尝试使用ObjectId作为主键:

Workaround: Try to use ObjectId as a primary key:

Orders= new Meteor.Collection('Orders', {idGeneration: 'MONGO'});

这篇关于Mongo在Meteor应用程序的_id_字段上给出重复的密钥错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆