score:4
You don't have to look for it. When you insert a new object it's ID is generated on the client side, that means you know it already before sending.
From here: http://docs.mongodb.org/manual/reference/object-id/
ObjectId is a 12-byte BSON type, constructed using:
a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and a 3-byte counter, starting with a random value.
So, when you request to insert a new document, you will specify it already. Usually you either use existing ID, or you generate a new one using BSONObjectID.generate.
Here is a quick draft using a custom class for mapping(in this example I was using reactivemongo, not casbah):
case class Account(
id: Option[BSONObjectID],
firstName: String,
lastName: String)
And then you do the following in your writer:
implicit object AccountBSONWriter extends BSONDocumentWriter[Account] {
def write(account: Account): BSONDocument =
BSONDocument(
"_id" -> account.id.getOrElse(BSONObjectID.generate),
"first_name" -> account.firstName,
"last_name" -> account.lastName)
}
Source: stackoverflow.com
Related Query
- Inserting Object into Mongo with _id Output
- Inserting an object into Mongo database using Scala's ReactiveMongo driver
- Slick - Inserting a row into two tables linked with an auto-incrementing key?
- Inserting multiple values into table with anorm
- Scala Slick, Trouble with inserting Unicode into database
- Is it possible to inject into an scala object with scaldi dependecy injection?
- Inserting into Cassandra with Akka Streams
- How to "reads" into a Scala Case Class given a Json object with key names that start with a capital letter
- MongoTypeConversionException: Cannot cast STRING into a NullType with Mongo Spark Connector even when explicit schema does not contain NullTypes
- How can I parse a JSON object with key-value objects into a List using Circe
- How to update object in Mongo with an immutable Salat case class
- issues with running script to read from mysql and insert into mongo (multithreading issues) using play framework
- How to put datas from MongoDB into an object with Casbah and Scala
- Inserting JsNumber into Mongo
- How to prevent debugger from stepping into Object class code in Eclipse when developing with Scala?
- How to parse a csv with matching case class and store the output to treemap[Int, List[List[InputConfig]] object in scala
- scala.html template file not compiled into object with apply() method (i.e. template.scala file)
- Slick MSSQL inserting object with auto increment
- Inserting multiple values and fields into table with anorm
- Split list into multiple lists with fixed number of elements
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- how to make saveAsTextFile NOT split output into multiple file?
- How to insert double quotes into String with interpolation in scala
- How to load 100 million records into MongoDB with Scala for performance testing?
- Reading TSV into Spark Dataframe with Scala API
- Merge Spark output CSV files with a single header
- Why does sbt build fail with "MissingRequirementError: object scala.runtime in compiler mirror not found."?
- Get companion object instance with new Scala reflection API
- Shapeless - turn a case class into another with fields in different order
- Serializing a scala object into a JSon String using lift-json
More Query from same tag
- TestActorRef: Could not get the underlyingActor, says Nothing
- How to transform the dataframe into label feature vector?
- Scala is boxing and unboxing non parametric function arguments and result. Why? Posible duplicate
- Access SQL Server 2008 R2 via Scala/JDBC on Windows 2008 64 bit using integrated authentication
- should I put the jar dependencies on the shared repo or just the source files?
- What might cause linking errors that disappear on clean in fastOptJS?
- Objects Extending Scala traits. Ruby equivalent?
- How can I architect the following inheritance problem in Scala?
- ArrayBuffer not persisting values outside loop in scala
- How to view the innards of a Java object in scala?
- Scala Actors: Is there a built-in way to stop/ interrupt an actor?
- Scala equivalent of Ruby's map.each?
- Parsing XML to typed values: xsd2scala?
- How to stop Spark from loading defaults?
- Serialize to object using scala mongo driver?
- Spark 2.1.0, cannot resolve column name when doing second join
- Calculate average over RDD[Vector] in Spark
- How can i export ENV variable with SBT?
- How can I create a DSL involving "blocks" where certain functions are in-scope?
- Generic function where the return type depends on the input type in Scala?
- Scala generics implementing Ordered[A[B]] gives strange compiler errors
- Does using scala's type declaration for clarity have and drawbacks?
- How to use NOT IN clause in filter condition in spark
- Why is this code depending on `withFilter` or is this a bug?
- NoSuchMethodError: scala.Predef$.$conforms()Lscala/Predef$$less$colon$less
- Why does DataFrame.map throw Nothing match exception?
- Compilation error: error: object scalatest is not a member of package.org, am NOT using sbt
- Scala - Vals and Defs - Decompiled
- Matching cardinality of varargs when passing parameters
- Override toString behavior for Option[_] in scala