简体   繁体   中英

using $in in find() query in casbah

I'm trying to use casbah's fluid querying in find().

My data is like this:

{ "_id" : ObjectId("4d7b26efc58bf2b18f14d9cd"), "srcID" : [ "/m/05zppz" ], "relation" : [ "/location/location/people_born_here" ], "dstID" : [ "/m/06sfnt9" ] }
{ "_id" : ObjectId("4d7b26efc58bf2b19014d9cd"), "srcID" : [ "/m/05zppz" ], "relation" : [ "/location/location/people_born_here" ], "dstID" : [ "/m/06fqp8" ] }
{ "_id" : ObjectId("4d7b26efc58bf2b19114d9cd"), "srcID" : [ "/m/05zppz" ], "relation" : [ "/location/location/people_born_here" ], "dstID" : [ "/m/06_7xfd" ] }

I wrote the following code to query this:

val srcIDs:List[String] = List("/m/05zppz", "/m/06sfnt9")
val query = "srcID" $in srcIDs

the code segment does not compile and reports this error:

error: value $in is not a member of java.lang.String
query = ("srcID" $in srcIDs)

The casbah documentation has the above syntax for $in, but it doesn't seem to work. How to make the $in query work? As a matter of fact, I could not get any fluid query with Casbah's DSL to work and they fail giving the same error msg. Please help!

The problem is that $in is not a method defined on strings. Most likely Casbah defines an implicit conversion from String objects to instances of a class that contain the method $in . The implicit conversion needs to be import ed into scope before it can be used.

Could you point us to the Casbah documentation that introduces the $in method? That's where we will find what needs to be imported.

(For the experts: isn't $ supposed to be reserved for compiler generated fields?)

I fixed it! You reply was very helpful. I had missed the import statement needed for implicit conversion and hence it was treating it as a String.

I included import com.mongodb.casbah.Imports._ and it works like a charm now. Thanks for the hint!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM