想象一下,我有一个包含以下字段的MongoDB集合:
buildingID
(String)projectID
(String)coords
(经度/纬度坐标数组)我有很多记录,通过projectID
属性 - 分配给项目A.现在我想
projectID
以外的所有字段都等于原始字段和projectID
等于项目B。我可以这样做:
Collection coll = getDb().getCollection("MyColl");
final Map<String,Object> query = new HashMap<>();
query.put("projectid", "projectA");
DBCursor cursor = coll.find(new BasicDBObject(query));
while (cursor.hasNext()) {
final BasicDBObject curRecord = cursor.next();
final BasicDBObject newRecord = clone(curRecord);
newRecord.set("projectid", "projectB");
coll.insert(newRecord);
}
clone
会创建curRecord
?
有更优雅的方法吗?我可以避免将数据从MongoDB中导入Java并重新导入MongoDB吗?
答案 0 :(得分:1)
确实有更优雅的方式来做到这一点。使用Bulk Operations API,因为这会大大减少对服务器的写入和响应次数:
BulkWriteOperation bulk = coll.initializeOrderedBulkOperation();
Integer count = 0;
DBCursor cursor = coll.find(new BasicDBObject("projectid", "projectA"));
while (cursor.hasNext()) {
DBObject curRecord = cursor.next();
curRecord.removeField("_id"); // why bother with a clone when you can remove the _id
curRecord.put("projectid","projectB"); // replace the projectid
bulk.insert(curRecord);
count++;
if ( count % 1000 == 0 ) {
bulk.execute();
bulk = collection.initializeOrderedBulkOperation();
}
}
if (count % 1000 != 0 )
bulk.execute();
现在每1000次操作只会从服务器发送/接收内容。这也是一个内部限制,但它有助于限制内存消耗以自行管理。