Best way to create bulk upsert

264 views
Skip to first unread message

miki haiat

unread,
Jan 6, 2015, 10:22:08 AM1/6/15
to mongodb-ca...@googlegroups.com
Hey ,

im using BulkWriteOperation in order to insert bulk Documents ,this is my code

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()


Is there any way to create Bulk Upsert ?

thanks,

miki

Ross Lawley

unread,
Jan 9, 2015, 9:38:10 AM1/9/15
to mongodb-ca...@googlegroups.com
For prosperity and google searches - I answered here http://stackoverflow.com/questions/27805297/best-way-to-create-bulk-upsert-with-casbah/27815879#27815879

Yes the find operation returns a BulkWriteRequestBuilder which allows you to replaceremoveupdate 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() 

Ross
Message has been deleted

Vivek YS

unread,
Feb 25, 2015, 6:18:14 AM2/25/15
to mongodb-ca...@googlegroups.com

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)

Ross Lawley

unread,
Feb 25, 2015, 8:49:28 AM2/25/15
to mongodb-ca...@googlegroups.com
Hi Vivek,

Can you post this in a new thread? I don't think its related to this one.  In the new post could you also add what Record.toBson(record) returns - I think that is the issue.

Ross
Reply all
Reply to author
Forward
0 new messages