0

Lets say I have

case class Foo(_id: BSONObjectID, bars: Set[Bar])
case class Bar(_id: BSONObjectID, name: String)

How can I get the bars of a specific Foo using projections?

I tried (omitting the declaration of the handlers)

val bars: Future[Option[Set[Bar]]] = fooes.find(
  BSONDocument("_id" -> BSONObjectID("specificId")), // Match condition
  BSONDocument("_id" -> 0, "bars" -> 1)              // Projection
).cursor[Bar]().collect[Set]()

But it doesn't work.

Regards

Quarktum
  • 669
  • 1
  • 8
  • 26
  • If you consider each document only for its `bars` field being a `Set[Bar]`, the cursor is not a `Bar` one, but a `.cursor[Set[Bar]]` and so you have to have a `BSONReader` available in the scope. – cchantep Nov 30 '15 at 13:44
  • @cchantep I have tried that, but I obtain `could not find implicit value for parameter reader: reactivemongo.bson.BSONDocumentReader[Set[Bar]]`. Am I missing an import? – Quarktum Nov 30 '15 at 14:33
  • @cchantep I omitted in the description the following (which I think it is obvious, but maybe it is not) `implicit val bsonF = Macros.handler[Bar]`. – Quarktum Nov 30 '15 at 14:37
  • There is no standard `BSONReader` provided by `Set` (as it can be unsafe), so either you go through `List` or you implemented your own `Set` reader. – cchantep Nov 30 '15 at 15:01
  • @cchantep Sorry, but it is not working yet :( `could not find implicit value for parameter reader: reactivemongo.bson.BSONDocumentReader[List[Bar]]`. – Quarktum Nov 30 '15 at 15:17
  • 1
    There is no `BSONDocumentReader` provided for such edge case. You have to implement your own `implicit def myEdgeReader(implicit br: BSONReader[_ <: BSONValue, Bar]): BSONDocumentReader[Set[Bar]] = new BSONDocumentReader[Set[Bar]] { def read(d: BSONDocument): Set[Bar] = d.asTry[List[Bar]]("bars").map(_.toSet).get } }` – cchantep Nov 30 '15 at 22:39

0 Answers0