MongoDb 2.4.7
蒙戈-java的驾驶员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();
答案 0 :(得分:0)
您将以{“recnum”:99}结束相同对象列表(所有列表条目将引用同一对象), 似乎并不是你想做的事情。您需要创建新的BasicDBObject 每个for循环迭代,否则你将在每次迭代时修改相同的jobRecord。
这同样适用于逐个方法:您正在观察不同的“recnum”字段,因为该对象在修改之前已经插入到Mongo中。