MongoDb 2.4.7
mongo-java-driver-2.11.2.jar
Windows 7的
所以我试图通过插入一批插入来加载测试 mongodb 来判断它可以处理多少吞吐量。如果一次插入一个它可以工作:
...
MongoClient conn = new MongoClient("localhost", 27017);
DB db = conn.getDB( "myDb" );
BasicDBObject jobRecord = new BasicDBObject(...);
DBCollection coll = db.getCollection("myTable");
BasicDBObject finalJobRecord;
for(int i=0;i<100;i++)
{
finalJobRecord = jobRecord;
finalJobRecord = finalJobRecord.append("recnum",String.valueOf(i));
coll.insert(finalJobRecord);
finalJobRecord.removeField("_id");// wont work without because of duplicate key errors
}
conn.close();
现在,如果我将其更改为批处理它不起作用(重复键异常触发):
...
MongoClient conn = new MongoClient("localhost", 27017);
DB db = conn.getDB( "myDb" );
BasicDBObject jobRecord = new BasicDBObject(...);
DBCollection coll = db.getCollection("myTable");
BasicDBObject finalJobRecord;
List<DBObject> basicDBObjects = new LinkedList<>();
for(int i=0;i<100;i++)
{
finalJobRecord = jobRecord;
finalJobRecord = finalJobRecord.append("recnum",String.valueOf(i));
basicDBObjects.add(finalJobRecord);
}
coll.insert(basicDBObjects);
conn.close();