Request for null using Casbah

I use Casbah to connect to the Mongo database and query. Right now I am trying to query for fields where the value is non-zero.

Using Mongo's direct syntax, the query I want will be collection.find({"key" : {$ne : null }} )

In casbah, I tried collection.find("key" $ne null) , which does not compile, and collection.find("key" $ne MongoDBObject(null)) , which throws a null pointer exception at runtime, as well as collection.find("key" $ne org.bson.BSON.NULL) , which is not really requesting properly.

collection.find("key" -> null) will work to query those that are null.

In the examples above, my syntax could be discarded, but the main idea should be conveyed.

Any ideas anybody?

I cannot change the database so that null records simply do not exist. This is out of control. I can change to not casbah, but I really would not want to.

+4
source share
2 answers

You can often use None for null in scala. Here you want:

 collection.find("key" $ne None) 
+1
source
 collection.find(MongoDBObject("key" -> MongoDBObject("$ne" -> None))) 

It does the job. I am also stuck on this.

+1
source

Source: https://habr.com/ru/post/1498472/


All Articles