MongoDB在执行后重新初始化BulkWriteOperation

时间:2015-02-26 18:28:22

标签: mongodb jdbc bulkinsert

我首先初始化BulkWriteOperation并通过for循环向其添加几个插入。然后我执行。然后我重新初始化BulkWriteOperation并尝试添加更多插入,但我一直得到:

java.lang.IllegalStateException: already executed

我的代码:

BulkWriteOperation builder = coll.initializeOrderedBulkOperation();

for( int i = 0; i < 10; i++ ) {
    BasicDBObject doc = new BasicDBObject("Something", something);
    builder.insert(doc);
}

builder.execute();

builder = coll.initializeOrderedBulkOperation();

for( int i = 0; i < 10; i++ ) {
    BasicDBObject doc = new BasicDBObject("Something", something);
    builder.insert(doc);
}

builder.execute();

1 个答案:

答案 0 :(得分:0)

执行它之后,没有办法重置现有的BulkWriteOperation对象,所以你只需要创建一个像这样的新对象:

builder = coll.initializeOrderedBulkOperation();