Skip to content

Commit

Permalink
[reactivemongo] $in extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
cchantep committed Sep 26, 2014
1 parent d213d6d commit f5e58c0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion reactive-mongo/readme.md
Expand Up @@ -268,9 +268,13 @@ val queryHandler = QueryHandler { queryRequest =>
// Matching on count query // Matching on count query
resultK resultK


case CountRequest(_, ("property", InClause(
BSONString("A") :: BSONString("B") :: Nil)) :: Nil) =>
resultL // matches count with selector on 'property' using $in operator

case Request("col1", SimpleBody(("$in", ValueList(bsonA, bsonB)) :: Nil)) => case Request("col1", SimpleBody(("$in", ValueList(bsonA, bsonB)) :: Nil)) =>
// Matching BSONArray using with $in operator // Matching BSONArray using with $in operator
resultL resultM
} }
} }
``` ```
Expand Down
24 changes: 24 additions & 0 deletions reactive-mongo/src/main/scala/acolyte/reactivemongo/Request.scala
Expand Up @@ -116,6 +116,13 @@ object SimpleBody {


} }


/** Complete request body extractor; Matches body with many documents. */
object RequestBody {
/** @return List of document, each document as list of its BSON properties. */
def unapply(body: List[BDoc]): Option[List[List[(String, BSONValue)]]] =
Some(body.map(_.underlying.elements.toList))
}

/** /**
* Extractor of properties for a document used a BSON value * Extractor of properties for a document used a BSON value
* (when operator is used, e.g. `{ 'age': { '\$gt': 10 } }`). * (when operator is used, e.g. `{ 'age': { '\$gt': 10 } }`).
Expand Down Expand Up @@ -154,6 +161,23 @@ object CountRequest {
} }
} }


/**
* In clause extractor
* (\$in with BSONArray; e.g. { '\$in': [ ... ] })
*/
object InClause {
/**
* Matches BSON property with name \$in and a BSONArray as value,
* and extracts subvalues from the array.
*/
def unapply(bson: BSONValue): Option[List[BSONValue]] =
bson match {
case ValueDocument(("$in", a @ BSONArray(_)) :: _)
Some(a.values.toList)
case _ None
}
}

/** /**
* Meta-extractor, to combine extractor on BSON properties. * Meta-extractor, to combine extractor on BSON properties.
* @see SimpleBody * @see SimpleBody
Expand Down
Expand Up @@ -329,6 +329,14 @@ object RequestSpec extends org.specs2.mutable.Specification
BSONDouble(c) :: Nil) ok BSONDouble(c) :: Nil) ok
} }
} }

"be extracted from an $in clause" in {
BSONDocument("selector" -> BSONDocument("$in" -> BSONArray("A", "B"))).
aka("body") must beLike {
case ValueDocument(("selector", InClause(
BSONString("A") :: BSONString("B") :: Nil)) :: Nil) ok
}
}
} }
} }


Expand Down

0 comments on commit f5e58c0

Please sign in to comment.