1

我正在使用 pentaho 数据集成工具从 SQL Server 数据库填充 Mongodb 集合。mongodb 集合中没有定义索引。由于在集合中创建了重复的 id,因此作业失败。

我想知道在哪种情况下可能会导致问题。任何建议都会非常有帮助。

occurred during write: com.mongodb.MongoException$Network: Write operation to server /myhost:27017 failed on database transactions

    ERROR (version 5.0.1-stable, build 1 from 2013-11-15_16-08-58 by buildguy) : An error     occurred during write: com.mongodb.MongoException$DuplicateKey:
   { "serverUsed" : "/myhost:27017" , "connectionId" : 186 , "err" :
   "insertDocument :: caused by :: 11000 E11000 duplicate key error
   index: transactions.datacollection.$_id_  dup key: { :
   ObjectId('534fe5644503839b0f6d47a2') }" , "code" : 11000 , "n" : 0 ,
   "ok" : 1.0} 

ERROR (version 5.0.1-stable, build 1 from 2013-11-15_16-08-58 by buildguy)
   : An error occurred during write:
   com.mongodb.MongoException$DuplicateKey: { "serverUsed" :
   "/myhost:27017" , "connectionId" : 186 , "err" : "insertDocument ::
   caused by :: 11000 E11000 duplicate key error index:
   transactions.datacollection.$_id_  dup key: { :
   ObjectId('534fe5644503839b0f6d47a2') }" , "code" : 11000 , "n" : 0 ,
   "ok" : 1.0}  

   ERROR (version 5.0.1-stable, build 1 from 2013-11-15_16-08-58 by buildguy)
   : An error occurred during write: com.mongodb.MongoException$Network:
   Write operation to server /myhost:27017 failed on database
   transactions

   MongoDB Output.0 - ERROR (version 5.0.1-stable, build 1 from
   2013-11-15_16-08-58 by buildguy) : An error occurred during write:
   com.mongodb.MongoException$DuplicateKey: { "serverUsed" :
   "/myhost:27017" , "connectionId" : 187 , "err" : "insertDocument ::
   caused by :: 11000 E11000 duplicate key error index:
   transactions.datacollection.$_id_  dup key: { :
   ObjectId('534fe5644503839b0f6d47a2') }" , "code" : 11000 , "n" : 0 ,
   "ok" : 1.0}

   ERROR (version 5.0.1-stable, build 1 from 2013-11-15_16-08-58 by buildguy) : Unexpected   error ERROR (version 5.0.1-stable, build 1 from
   2013-11-15_16-08-58 by buildguy) :
4

2 回答 2

0

默认情况下,如果您不提供 _id 字段,则 mongodb(驱动程序或守护程序)会自动添加。_id 使用唯一属性进行索引。请参阅http://docs.mongodb.org/manual/reference/method/db.collection.insert/

如果驱动程序像 java 驱动程序一样处理这些事情(至少对于最新版本),并且您执行以下操作

    MongoClient c =  new MongoClient(new MongoClientURI("mongodb://somehost"));
    DB db = c.getDB("someDB");
    DBCollection coll = db.getCollection("someCollection");
    BasicDBObject obj = new BasicDBObject("field1", "value1");

    coll.insert(obj);

    obj.removeField("field1");
    obj.append("field1", "value2");
    coll.insert(obj);
    //maybe in a loop

你就麻烦了,因为第一次调用insert,_id是没有生成的,后来就不用重新生成了。您一直在使用相同的 _id。您可以创建一个新的 basicDBObject 删除字段 _id 来解决问题。

于 2014-04-18T19:53:10.727 回答
0

对于生成随机数据并插入 MongoDB 的模拟应用程序,我遇到了同样的麻烦,如果这有帮助;每次调用 BasicDBObject 的插入操作时,在函数结束时您也会调用“clear()”操作。

于 2015-03-24T09:21:44.097 回答