在 casbah 中,我看到 MongoCollection 上定义了以下函数:
def
insert[A](docs: A*)(implicit dbObjView: (A) ⇒ commons.TypeImports.DBObject, concern: mongodb.WriteConcern = writeConcern, encoder: TypeImports.DBEncoder = ...): TypeImports.WriteResult
如果我理解正确,您可以将多个 DBObject 传递给此函数以进行批量插入。
我关心/问题是这是一个参数列表。如果我有大量插入物(比如说几百个)怎么办?通过这种方式传递的对象是否有限制?
最佳答案
单个命令中可使用的文档大小有限制 ~16mb,因此通过该路径可以执行的插入数量也有限制。
但是,如果您使用 Casbah 2.7 中针对 MongoDB 2.6 发布的 BulkOperationBuilders
之一,它会自动将操作拆分为多个批处理,以便您可以执行更大的批量操作:
val collection = MongoClient()("test")("bulkOperation")
collection.drop()
// Ordered bulk operation
val builder = collection.initializeOrderedBulkOperation
builder.insert(MongoDBObject("_id" -> 1))
builder.insert(MongoDBObject("_id" -> 2))
builder.insert(MongoDBObject("_id" -> 3))
builder.find(MongoDBObject("_id" -> 1)).updateOne($set("x" -> 2))
builder.find(MongoDBObject("_id" -> 2)).removeOne()
builder.find(MongoDBObject("_id" -> 3)).replaceOne(MongoDBObject("_id" -> 3, "x" -> 4))
val result = builder.execute()
存在有序或无序操作,请参阅:http://mongodb.github.io/casbah/whats_new.html#ordered-unordered-bulk-operations
关于mongodb - 如何进行casbah批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23684938/