converting the $in to a query

153 views
Skip to first unread message

Akka Chelley

unread,
Apr 4, 2014, 12:50:57 AM4/4/14
to reacti...@googlegroups.com

Was reading the mongodb documentation.

  • db.CollectionName.find( { qty: { $in: [ 5, 15 ] } } )
Any idea how this translates to a query to my basically defining the "query"

val f : Future[Seq[Foobar]] = somCollection.find(query).cursor[FOO].collect[Seq]()

I tried this -- doesn't seem to work.

    val query = BSONDocument(
      "mygroupId" -> BSONDocument(
        "$in" -> BSONArray(someIds.map(convertoBSONString).toStream)
      )
    )

Thanks
_D

Jaap Taal

unread,
Apr 5, 2014, 9:35:58 AM4/5/14
to reacti...@googlegroups.com
Hey there,

If you import reactivemongo.bson._ you get the implicit function collectionToBSONArrayCollectionWriter into scope. It will basicly allow you to implicitly convert a collection into a BSONArray.

You should be able to write your query as:

val query = BSONDocument(
  "mygroupId" -> BSONDocument(
    "$in" -> someIds
  )
)

Jaap

Jaap Taal

unread,
Apr 5, 2014, 9:37:55 AM4/5/14
to reacti...@googlegroups.com
BTW this implicit magic isn't very well documented, at least I haven't really seen it. I've learned about the implicit conversions available by looking at the source:

Akka Chelley

unread,
Apr 14, 2014, 3:06:53 PM4/14/14
to reacti...@googlegroups.com
This works brilliantly! thanks Jaap Taal

_D
Reply all
Reply to author
Forward
0 new messages