Basic construct to query on mongo is query
command. Query command creates process of BSONObjects, that can be further
processed by scalaz.stream process combinators. Query supports simple DSL that can be further combined to produce remove, update and modify operations.
to make a simple query that returns process of BSONObjects you can do
query("key" -> "value")
if you want limit the amount of the documents returned, you can use limit
and skip
combinators
query("key" -> "value") skip 1 limit 3
if you want to sort documents before returning them back, you can use sort
combinator. Sort combinator allows you to specify order and multiple keys
that will be used when sorting. Please note two syntax for ordering allowed
query("key" -> "value") sort("key" Ascending, "otherKey" -> Order.Descending)
query("key" -> "value") orderby("key" Ascending, "otherKey" -> Order.Descending)
Constructed query may contain javascript queries that build mongodb $where
query. The passed in javascript is evaluated
on server side, thus there is no syntax or correctness check in driver.
query("key" >= 10 ) where("this.key *2 < 33")
MongoDb allows various functionality when working with replica sets. Essentially, the possible options are described in ReadPreference in http://docs.mongodb.org/manual/core/read-preference/.
All of these options are supported via from
combinator:
import scalaz.stream.mongodb.query.ReadPreference._
query("key" -> "value") from Nearest.preferred(false)
In mongoDB environments you may potentially want to read certain data only with specific members. This can be be achieved by tagging the read preference :
import scalaz.stream.mongodb.query.ReadPreference._
query("key" -> "value") from Nearest.preferred(true).tags("disk" -> "ssd", "memory" -> "huge")
For the complex documents you maybe want to get only certain data as a result of the query to reduce time when deserializing data. This is achieved
with project combinator:
query("key" -> "value") project("key1" include, "key2" $, "key3" first 4, "key4" last 6, "key5" skip 6 limit 5, "key6" elementMatch ("ek" -> 1))
As you may see projection combinator acepts many form of limiting the content of documents returned. Lets describe their meaning
For more information you can see detailed description of their functionality in mongoDB docs http://docs.mongodb.org/manual/reference/operator/projection/
To return only key1 from documents :
query() project ("key1" include)
To return only first element of any array under key1:
query("key3" >= 1) project ("key3" $)
To return only first 4 elements of any array under key1:
query() project ("key1" first 4)
To return only last 4 elements of any array under key1:
query() project ("key1" last 4)
To return elements 4-6 from an array under key1:
query() project ("key1" skip 3 limit 3)
To return elements that have ek == 1 under key 1:
query() project ("key1" elementMatch ("ek" === 1))
To see the actual indexes that are consulted when running the query and additional statistics, query may be run in explain mode.
See http://docs.mongodb.org/manual/reference/method/cursor.explain/#cursor.explain:
query("key" -> "value") explain (ExplainVerbosity.Normal)
or
query("key" -> "value") explain (ExplainVerbosity.Verbose)
Query can contain index hints to choose appropriate index for given query.
This indicates index named "indexName" shall be used
query("key" -> "value") hint ("indexName")
or this picks index on "key1" to be used with query
query("key" -> "value") hint (index("key1" Ascending))
Query can be in "snapshot" mode to make sure that documents are not returned multiple times when iterating over results.
Queries with size less than 1M are automatically in snapshot mode.
query("key" -> "value") snapshot (true)
Query can be commented to have given comment appear in profile log
query("key1" -> "value") comment ("Long duration query")
Total for specification CollectionQuerySpec | |
---|---|
Finished in | 36 seconds, 477 ms |
Results | 15 examples, 1500 expectations, 0 failure, 0 error |