EzDevInfo.com

casbah

Officially supported Scala Driver for MongoDB MongoDB API Docs for scala/casbah

Compilation failed: error while loading AnnotatedElement, ConcurrentMap, CharSequence from Java 8 under Scala 2.10?

I'm using the following:

  • Scala 2.10.4
  • Scalatra 2.2.2
  • sbt 0.13.0
  • java 1.8.0
  • casbah 2.7.2
  • scalatra-sbt 0.3.5

I'm frequently running into this error:

21:32:00.836 [qtp1687101938-55] ERROR o.fusesource.scalate.TemplateEngine - Compilation failed:
error: error while loading CharSequence, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar(java/lang/CharSequence.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 18 at byte 10)
error: error while loading ConcurrentMap, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar(java/util/concurrent/ConcurrentMap.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 18 at byte 61)
two errors found
21:38:03.616 [qtp1687101938-56] ERROR o.fusesource.scalate.TemplateEngine - Compilation failed:
error: error while loading AnnotatedElement, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar(java/lang/reflect/AnnotatedElement.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 18 at byte 76)
one error found

Currently I'm running into this when simply trying to call a .count() on my MongoDB collection.

Upon Googling, it seems like it may be caused by dependency issues. The thing is, I'm using Scalatra just to serve an API and actually don't require any of the scalate stuff. I commented out all references to it, but I still get this. Could it be a dependency issue between the libraries I'm using?

Any help appreciated. Thanks!


Source: (StackOverflow)

Why do I end up with java.lang.IllegalArgumentException for Casbah / Java MongoDB Driver?

I'm seeing a strange issue using the casbah / java driver.

I keep running into the following exception when the driver tries to create a response from mongo:


  Oct 16, 2012 10:45:07 AM com.mongodb.DBTCPConnector$MyPort error
  SEVERE: MyPort.error called
  java.lang.IllegalArgumentException: response too long: 1634610484
    at com.mongodb.Response.(Response.java:40)
    at com.mongodb.DBPort.go(DBPort.java:110)
    at com.mongodb.DBPort.go(DBPort.java:75)
    at com.mongodb.DBPort.call(DBPort.java:65)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:201)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
    at com.mongodb.DBCursor._check(DBCursor.java:354)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:484)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:509)
    at com.mongodb.casbah.MongoCursorBase$class.hasNext(MongoCursor.scala:73)
    at com.mongodb.casbah.MongoCursor.hasNext(MongoCursor.scala:497)
    at scala.collection.Iterator$class.foreach(Iterator.scala:660)
    at com.mongodb.casbah.MongoCursor.foreach(MongoCursor.scala:497)
        ...

This seems to be happening at random, even in cases where mongo shouldn't be returning any results from the query. The size reported is inconsistent and normally way larger than it should or could be.

I'm guessing this is probably some form of corruption with the response. I've had a difficult time making heads or tails of this problem using Wireshark, but I consistently find that the request/response chain leading up to the query whose response throws this error has "TCP Window Full" response coming from MongoDB:

enter image description here

The versions I'm using:


 Casbah: 2.1.5.0
 Mongo Java Driver: 2.5.3
 MongoDB: 2.2

I'd greatly appreciate it if anyone could give any advice on what I may potentially be doing wrong or any pointers on how to further debug this issue

UPDATE: After looking at the release notes for Casbah 2.1.5.0, it appears that the version of the java driver being used is 2.5.3, not 2.7.3


Source: (StackOverflow)

Advertisements

mongodbimport Failure - FailedToParse: Expecting '{': offset:0

I tried to import a 7.4 MB JSON file via ...

mongoimport -d mongoimport -c test --file jsonTest.json

But I saw this issue.

Wed Sep 04 13:08:52.378 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0

This Stackoverflow post presented a similar issue with respect to the date occurring before Jan 1, 1970, but that doesn't seem to apply given my FailedToParse: Expecting '{': offset:0 error.


Source: (StackOverflow)

How to pool the connections of mongodb with casbah?

I'm using mongodb with scala driver casbah. If I'm not understanding wrong, it doesn't provide connection pool. Is there any pool libraries for casbah, like dbcp/c3p0 for jdbc connections?


Source: (StackOverflow)

Why do my MongoDB simultaneous $push updates fail?

I'm performing a number of updates of the form

update(
  { "uuid": someUuid, "revision.versionNumber": someVersionNumber},
  { "$set": { "meta.someId": someId }, "$push": { "meta.someMessages": someMessage } }
)

occasionally I see when this is called for the same uuid,versionNumber, & someId with a different someMessage the first update will succeed but the second will fail silently.

I see the following in the mongo logs so I know the updates are making it to the database, notice that the first update has the same query as the third one but the first has nupdated: 1 while the third has nupdated: 0

Wed Aug 28 14:50:24 [conn18] update some-db.some_collection query: { uuid: "b841f303-a054-4eb9-8885-9d3ebf9906a1", revision.versionNumber: 9 } update: { $set: { meta.someId: "521e6fe4036420f90371a922" }, $push: { meta.someMessages: { event: "instance.complete", timestamp: new Date(1377726624985) } } } nscanned:2507 nmoved:1 nupdated:1 keyUpdates:0 numYields: 19 locks(micros) w:6010 9ms
Wed Aug 28 14:50:24 [conn18] run command some-db.$cmd { getlasterror: 1, fsync: true }
Wed Aug 28 14:50:24 [conn14] update some-db.some_collection query: { uuid: "843f424d-8a62-4a8b-853f-dc2e9c42b309", revision.versionNumber: { $lt: 10 }, meta.deleted: true } update: { $set: { meta.deleted: false } } nscanned:3243 nupdated:0 keyUpdates:0 numYields: 23 locks(micros) w:8431 11ms
Wed Aug 28 14:50:24 [conn14] run command some-db.$cmd { getlasterror: 1, fsync: true }
Wed Aug 28 14:50:24 [conn5] update some-db.some_collection query: { uuid: "b841f303-a054-4eb9-8885-9d3ebf9906a1", revision.versionNumber: 9 } update: { $set: { meta.someId: "521e6fe4036420f90371a922" }, $push: { meta.someMessages: { event: "instance.complete.success", timestamp: new Date(1377726624985) } } } nscanned:3242 nupdated:0 keyUpdates:0 numYields: 20 locks(micros) w:5684 9ms

Also here is the output from mongosniff

update  flags:0 q:{ uuid: "85700d8c-8946-4b09-968b-968f76d31028", revision.versionNumber: 13 } o:{ $set: { meta.someId: "521e7b12036420f90371b515" }, $push: { meta.someMessages: { event: "instance.complete", timestamp: new Date(1377729439093) } } }
319 some-db.some_collection

    update  flags:0 q:{ uuid: "a460019d-443b-4b59-b23e-1eae19e26c31", revision.versionNumber: 14 } o:{ $set: { meta.someId: "521e7b2f036420f90371b579" }, $push: { meta.someMessages: { event: "task.start", timestamp: new Date(1377729439093) } } }
123 some-db.some_collection

    query: { uuid: "a2558f5c-d825-4ec4-bbc4-7e48b1cb3c60", isLatest: true }  ntoreturn: -1 ntoskip: 0
302 some-db.some_collection

    update  flags:0 q:{ uuid: "85700d8c-8946-4b09-968b-968f76d31028", revision.versionNumber: 13 } o:{ $set: { meta.someId: "521e7b12036420f90371b515" }, $push: { meta.someMessages: { event: "instance.complete.success", timestamp: new Date(1377729439093) } } }
173 some-db.some_collection

Source: (StackOverflow)

How to convert casbah mongodb list to json in scala / play

I'm learning scala and mongodb at present and using the play! framework, so I'm making all sorts of mistakes as I get my head around things. Currently I have a scala object that returns a list of database objects returned from a mongodb query via casbah as follows;

object Alerts  {

   def list() : List[DBObject]= {

        val collection = MongoDatabase.collection;
        val query = MongoDBObject.empty
        val order = MongoDBObject("Issue Time:" -> -1)
        val list = collection.find(query).sort(order).toList
        list
   }

... }

Elsewhere in my code I wish to output the List of objects in Json - so I have;

  val currentAlerts = Alerts.list()

What I'd like to write is something like;

  val resultingJson = currentAlerts.toJson 

But when I do this, I understandably get the following error;

  value toJson is not a member of List[com.mongodb.casbah.Imports.DBObject]

My question is - what's the right way to convert a List of com.mongodb.casbah.Imports.DBObject into Json for output?

EDIT:

For clarity, what I really want to do is the equivalent of

val listInJson = collection.find(query).sort(order).toJson

In the same way that I CAN write

val listAsString = collection.find(query).sort(order).toString

Source: (StackOverflow)

How to query a Array[String] for a regexp match?

i try to query MongoDB via Casbah for a field that is a array of strings with a regexp.

For example:

I have a Maschine with a list of ips, that are stored as string in the fields ips. Now i want to search for all machines that have the subnet 192.168.

For me i looks like that the i cannot query an array with a regexp applied to every entry and if one of the entries matches the machine is returned.

Any way to make such a query ?

-- Fixed

Thanks for your help.

Everything works now. At the end i need to work around one limitation of Casbah, because i needed to join to queries with $or and Casbah complains about missing implicits with the regexp.

My final code for a RegExp Array query with an additional other field is:

val regexp = ".*" + parameter + ".*"
val nameQ = MongoDBObject("serverName" -> regexp.r)
val ipsQ = MongoDBObject("ips" -> regexp.r)
val bldr = MongoDBList.newBuilder
bldr += ipsQ
bldr += nameQ
val query = MongoDBObject("$or" -> bldr.result.asDBObject)
val result = find(query)

It is not the nicest code and the string concatenation of the parameter needs to be fixed. But it works :)


Source: (StackOverflow)

find by regular expression with Casbah

how to use regular expressions at Collection#find(/* HERE */) like:

val coll = MongoConnection()("foo")("bar")
for(x <- coll.find("name" -> ".*son$".r)) {
   // some operations...
}

Source: (StackOverflow)

Casbah Scala MongoDB driver - getting data from a DBObject

Ok, so here is another question on basics of Casbah and MongoDB. After I have retreived a DBObject from database, how do I extract certain data from it? I know that there is the DBObject.get() method, that returns java.lang.Object. Do I have to do it like this and then just cast the data to the needed type? I am not sure that this is the best way to do it... can anyone recommend on how to do this better?

UPDATE:

Finally I went the way of manually handling all the stuff. I am not using Salat because of the case class limitation because case classes are not recommended for having children and that would require architectural rearrangement. However, the answer is marked as the best answer, since it would work in most situations and there is no other more general answer here.


Source: (StackOverflow)

How to clear/drop/empty a MongoDb collection with Casbah

I started using MongoDb in Scala via Casbah but cannot find on the Casbah documentation / google the way to drop the content of a collection. The MongoDd doc says the MongoDb shell command to do so is

db.things.remove({}); 

But how can we achieve the same via Casbah?

Thanks in advance,

Olivier


Source: (StackOverflow)

how to cast a Mongo BasicDBList to an immutable scala list

I have a BasicDBList that has been persisted into the database. I am now reading the data and trying to convert the list to an immutable scala list as shown:

val collection = mongoFactory.getCollection("tokens")    
val appId = MongoDBObject("appId" -> id)
val appDBObject = collection.findOne(appId) 
val scope: List[String] = appDBObject.get("scope").asInstanceOf[List[String]]

However, I am getting a class cast exception saying it is not possible to cast a BasicDBList to a Scala immutable list.

I have tried various combinations, such as converting to a map, etc. Nothing seems to work.


Source: (StackOverflow)

Casbah Scala MongoDB driver - embedded objects

I know that objects in MongoDB can contain multiple levels of data (just the way JSON objects can). However, the Casbah driver tutorial only covers the creation of "flat" objects, where there is just one level of data. How can I create and work with multilevel objects with Casbah?


Source: (StackOverflow)

How to retrieve all objects in a Mongodb collection including the ids?

I'm using Casbah and Salat to create my own Mongodb dao and am implementing a getAll method like this:

val dao: SalatDAO[T, ObjectId]    
def getAll(): List[T] = dao.find(ref = MongoDBObject()).toList 

What I want to know is:

  1. Is there a better way to retrieve all objects?
  2. When I iterate through the objects, I can't find the object's _id. Is it excluded? How do I include it in the list?

Source: (StackOverflow)

NoSQL (e.g. MongoDB) or RDMS (e.g. PostgreSQL) for new Scala project?

I'm developing a brand new project in Scala. It's just an application for a bunch of CRUD operations, however, because of some eccentric requirements, Play2 or Lift does not fit the bill, so I'm going to develop the application from the ground up. This means that Anorm or ScalaQuery becomes less obvious choices for database integration, and leaves me with the question: is it time to try something new?

My past technology stacks mostly included Java and PostgreSQL and I have experience with both ORM and plain SQL. Are NoSQL database management systems like MongoDB a good replacement for a typical RDBMS or are they special case application data stores? Also, how does the choice of database effect the greater Scala system design (if at all)? For example, the fact that you are using a JSON-like interface to talk to the database, and JSON between the web and a REST service, does not mean that much if everything in the middle becomes Scala objects, or does it?

I'm basically asking for someone's experience on moving from relational to object/document type databases, using Scala in particular. I know that good RDBMS integration is promised in the upcoming release of SLICK. So, if a company like TypeSafe decides to make a RDBMS integration part of the TypeSafe stack, then will I be swimming upstream by integrating to MongoDB using Casbah for example?

Apologies if this question appears a bit vague. I do hope that someone with the right insights or experience will be able to help though.

Update:

Apologies for not adding links to SLICK (it being fairly new). Here goes:

Update 2:

My personal first win for a technology is usually developer productivity - this translates to lightweight and simple: quick to learn, easy to maintain, no magic


Source: (StackOverflow)

SocketException in Mongo

I just set up a replica set in Mongo (prod environment). I'm now getting a lot of exceptions like below (clipped).

I went into mongo and ran a serverStatus command on my primary mongo node and only have about 300 connections going, so it's hardly working.

Below are my connection option settings in my server code:

auto_connect_retry = false    
connections_per_host = 10     
threads_multiplier = 10       
max_wait_time = 120000       
connect_timeout = 10000       
socket_timeout = 0           

Do I have something mis-configured?

Sep 9, 2013 8:31:26 PM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to /10.0.8.10:27017 b/c of error
java.net.SocketException: Connection timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:146)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at org.bson.io.Bits.readFully(Bits.java:46)
    at org.bson.io.Bits.readFully(Bits.java:33)
    at org.bson.io.Bits.readFully(Bits.java:28)
    at com.mongodb.Response.<init>(Response.java:40)
    at com.mongodb.DBPort.go(DBPort.java:142)
    at com.mongodb.DBPort.call(DBPort.java:92)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
    at com.mongodb.DBCollection.findOne(DBCollection.java:347)
    at com.mongodb.DBCollection.findOne(DBCollection.java:332)
    at com.mongodb.casbah.MongoCollectionBase$class.findOneByID(MongoCollection.scala:232)
    at com.mongodb.casbah.MongoCollection.findOneByID(MongoCollection.scala:866)
    at com.novus.salat.dao.SalatDAO.findOneById(SalatDAO.scala:353)
    at com.novus.salat.dao.ModelCompanion$class.findOneById(ModelCompanion.scala:173)

Source: (StackOverflow)