Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Last active December 24, 2015 07:19
Show Gist options
  • Select an option

  • Save kevinmeredith/6763397 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmeredith/6763397 to your computer and use it in GitHub Desktop.
Problem when trying to bulk insert in Casbah
// recurse through each element of "docs", using `builder` to add another key-value MongoDBObject for time-stamp purposes
def getEnrichedDocs(docs: List[DBObject]) : List[DBObject] = {
def go(list : List[DBObject], docs_: List[DBObject]) : List[DBObject] = list match {
case x :: xs => var builder = MongoDBObject()
builder = builder ++ MongoDBObject("LoadTimestamp" -> Utils.getTimestamp) // returns a Long (I know that I should be using a BSON TS, but that's a TODO
builder = builder ++ x
go(xs, docs_ :+ builder) // call go() recursively with the updated accumulator (2nd arg)
case Nil => docs_
}
go(docs, List[DBObject]())
}
val allDocs: List[DBObject] = getEnrichedDocs(getDocsList()) // getDocsList() unspecified here sincecontent not imp here
// Bulk insert
val docsList = MongoDBList(allDocs: _*) // avoid using a List of lists
collection.insert(docsList)
@kevinmeredith

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment