Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they开发者_如何学运维 tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionI want to use MongoDB with Scala. I found 2 libraries for it.
- lift-mongo
- mongo-scala-driver
Could you please tell me which library is the best to use? And please tell me other one if you know.
You should take a close look at Casbah, which is the official(and fully supported ) MongoDB Scala driver:
http://api.mongodb.org/scala/casbah/latest
Im admittedly biased as the creator and maintainer of the project but the official support brings a lot of benefit including the fact that there is a fulltime developer behind it. There are solid type conversion wrappers built in for common Scala types and Scala collections style implementations of DBObject which are much better than the default Java objects many of the other drivers use. There is also a full query dsl which maps to Mongo query syntax.
I like the Lift stuff as well and have recently started working w/ the Lift team to help improve it. Foursquare just released a query dsl for lift-mongo-record called Rogue which drives their own Scala+MongoDB system:
http://GitHub.com/foursquare/rogue
I have found most people are using either Lift or Casbah, but YMMV. Feel free to ping me if you need more help.
As an unbiased user of Casbah for a while now, I say, use Casbah for sure.
Check it:
val mongo = MongoConnection()
val coll = mongo("myDB")("myCollection")
val builder = MongoDBObject.newBuilder
builder += "username" -> "Janx"
builder += "comment" -> "Casbah is cool!"
coll += builder.result.asDBObject
That's of course just a taste. It's so refreshing to use especially if you come from the Java driver. And since it's now the Scala driver supported by 10Gen and the talented Mr. McAdams, it's really a no-brainer. Cheers!
We were sort of unsatisfied with the way Casbah works for deep objects or simple maps and no real case class mapping support so we rolled our own MongoDB Synchronous Scala driver on top of the legacy java driver which I would like to shamelessly plug here with an example on how to store and retrieve a map and a simple case class. The driver does not have a lot of magic and is easy to setup and features a simple BSON implementation which was inspired by the Play2 JSON impl.
Here is how to use it with some simple values:
val client = MongoClient("hostname", 27017)
val db = client("dbname")
val coll = db("collectionname")
coll.save(Bson.doc("_id" -> 1, "vals" -> Map("key1" -> "val1")))
val docOpt = coll.findOneById(1) // => Option[BsonDoc]
for(doc <- docOpt)
println(doc.as[Map[String, String]]("vals")("key1")) // => prints "val1"
And with a case class:
case class DnsRecord(host: String = "", ttl: Long = 0, otherProps: Map[String, String] = Map())
case object DnsRecord {
implicit object DnsRecordToBsonElement extends ToBsonElement[DnsRecord] {
def toBson(v: DnsRecord): BsonElement = DnsRecordToBsonDoc.toBson(v)
}
implicit object DnsRecordFromBsonElement extends FromBsonElement[DnsRecord] {
def fromBson(v: BsonElement): DnsRecord = DnsRecordFromBsonDoc.fromBson(v.asInstanceOf[BsonDoc])
}
implicit object DnsRecordFromBsonDoc extends FromBsonDoc[DnsRecord] {
def fromBson(d: BsonDoc): DnsRecord = DnsRecord(
d[String]("host"),
d[Long]("ttl"),
d[Map[String, String]]("op")
)
}
implicit object DnsRecordToBsonDoc extends ToBsonDoc[DnsRecord] {
def toBson(m: DnsRecord): BsonDoc = Bson.doc(
"host" -> m.host,
"ttl" -> m.ttl,
"op" -> m.otherProps
)
}
}
coll.save(DnsRecord("test.de", 4456, Map("p2" -> "val1")))
for (r <- coll.findAs[DnsRecord](Bson.doc("host" -> "test.de")))
println(r.host)
精彩评论