Mongodb Duplicate Exception用于非重复记录

时间:2014-09-22 03:50:32

标签: json mongodb duplicates

我将以下记录插入Mongo DB。

`{ "_id" : "531bbeae833b3d0977001597", "androidversioncode" : "16", "city" : "New York", "_t" : 1400921353 }`

我正在尝试插入另一条记录

`{"androidversioncode":"16","city":"Salvador","_t":1400930158}`

但是它会引发异常

  

错误:com.mongodb.MongoException $ DuplicateKey:{" serverUsed" :   "本地主机:27017" ,"错误" :" E11000重复键错误索引:   default.datarecords。$ id dup key:{:\" 531bbeae833b3d0977001597 \"   }" ,"代码" :11000," n" :0," connectionId" :28," ok" :1.0}

我的插入记录的代码在这里

while ((nextLine = br.readLine()) != null) {


  DBCollection collection = db.getCollection("datarecords");
  if (collection == null) {
    collection = db.createCollection("datarecords", null);
  }
  try {

     DBObject obj = (DBObject)JSON.parse(nextLine);

     collection.insert(obj);

   } catch(Exception e) {
        System.err.println("Could not create json object. " + nextLine + "Error: " + e);

   }

}

Mongo是否重复使用ID?

0 个答案:

没有答案