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()
Yes the find
operation returns a BulkWriteRequestBuilder
which allows you to replace
, remove
, update
and mark it as an upsert
request. So to perform an upsert you can do:
val builder: BulkWriteOperation = collection.initializeOrderedBulkOperation
builder.find(MongoDBObject("_id" -> 1)).upsert().updateOne($set("x" -> 2))
val result = builder.execute()
is it possible to upsert a complete bson doc ? The following code is throwing exception. I am not sure what exactly is the issue here.
val builder2 = context.test_coll.initializeOrderedBulkOperation
builder2.find(MongoDBObject("_id.x1" -> record._id.x1,
"_id.x2" -> record._id.x2,
"_id.x3" -> record._id.x3,
"_id.x4" -> record._id.x4)).upsert().update(Record.toBson(record))
builder2.execute()
Exception in thread "main" java.lang.IllegalArgumentException: Update document keys must start with $: _id at com.mongodb.DBCollectionImpl$Run.executeUpdates(DBCollectionImpl.java:769) at com.mongodb.DBCollectionImpl$Run.execute(DBCollectionImpl.java:734) at com.mongodb.DBCollectionImpl.executeBulkWriteOperation(DBCollectionImpl.java:149) at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:1737) at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:1733) at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:93) at com.mongodb.casbah.BulkWriteOperation$$anonfun$1.apply(BulkWriteOperation.scala:71) at com.mongodb.casbah.BulkWriteOperation$$anonfun$1.apply(BulkWriteOperation.scala:71) at scala.util.Try$.apply(Try.scala:161) at com.mongodb.casbah.BulkWriteOperation.execute(BulkWriteOperation.scala:71)