Bulk upsert

176 views
Skip to first unread message

Vivek YS

unread,
Mar 1, 2015, 12:19:50 AM3/1/15
to mongodb-ca...@googlegroups.com
Hi All,
    I am trying to perform the bulk upsert and I am getting this exception. 

java.lang.IllegalArgumentException: Update document keys must start with $: _id

Here is the code snippet 

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()
Also I am using salat for serializing and deserializing. Here is the code snippet for Record.toBson
grater[Record].asDBObject(record)
Record is defined as 
case class RecordKey (x1 : String, x2 : String, x3 : String, x4 : String)
case class Record (_id : RecordKey, x : String, y : String)
Exception stack trace
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,
Mar 2, 2015, 4:05:14 AM3/2/15
to mongodb-ca...@googlegroups.com
Hi,

Can you output what Record.toBson(record)  produces?

I think that might be the culprit

Ross

--
You received this message because you are subscribed to the Google Groups "mongodb-casbah-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-casbah-u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

name     : "Ross  Lawley",

  title    : "JVM Software Engineer",

  location : "London, UK",

  twitter  : ["@RossC0", "@MongoDB""@MongoDBInc"],

  facebook : ["MongoDB", "MongoDB, Inc."] }

Vivek YS

unread,
Mar 2, 2015, 5:38:28 AM3/2/15
to mongodb-ca...@googlegroups.com
I  think I found what the issue is. Casbah expects the kind of operation that is being performed. In this case, I am performing the set operation. 

So replacing 

update(Record.toBson(record))
with
update(new BasicDBObject("$set", Record.toBson(record)))
Solves the issue. Here is the output of Record.toBson(record)   
{ "_id" : { "x1" : "a" , "x2" : "b" , "x3" : "c" , "x4" : "d"} , "x" : "z"}

Here is the complete code snippet 

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(new BasicDBObject("$set", Record.toBson(record)))
Reply all
Reply to author
Forward
0 new messages