我正在使用BulkWriteOperation来插入批量文档,这是我的代码
val builder: casbah.BulkWriteOperation = collection.initializeOrderedBulkOperation
for(p<-posts){
if (p._1.\("object_id").asOpt[String].getOrElse("") != "" && p._1.\("message").asOpt[String].getOrElse("") !="")
builder.insert(MongoDBObject("id" -> p._1.\("id").asOpt[String], "object_id" -> p._1.\("object_id").asOpt[String], "message" -> p._1.\("message").asOpt[String],"shares"->p._1.\("shares").\("count").asOpt[Long].getOrElse(0),"likes"->p._2.\("summary").\("total_count").asOpt[Long].getOrElse(0)))
}
val result = builder.execute()
有没有办法创建Bulk Upsert?
感谢,
三木
答案 0 :(得分:0)
是find
操作返回BulkWriteRequestBuilder
,您可以replace
,remove
,update
并将其标记为upsert
请求。所以要执行upsert,你可以这样做:
val builder: BulkWriteOperation = collection.initializeOrderedBulkOperation
builder.find(MongoDBObject("_id" -> 1)).upsert().updateOne($set("x" -> 2))
val result = builder.execute()