Question

My query using ReactiveMongo:

collection
  .find(
    Json.obj("relateds" -> Json.obj("$elemMatch" -> activityRelated)),
    Json.obj("subscriberId" -> 1, "_id" -> 0)
  )
  .cursor[BSONObjectID]
  .collect[Seq]()

This is meant to return only one field "subscriberId" for each found document. The final output type should be Future[Seq[BSONObjectID]]

But it fails with:

Failure(java.lang.RuntimeException: JsError(List((,List(ValidationError(unhandled json value,WrappedArray()))))))

Any Ideas how to do this ?

Thanks in advance

Was it helpful?

Solution

Response for this query is sequence of objects, not values:

{ subscriberId : "foo" }

So, you need to define case class with single field for this id and then set cursor type as this case class or try something like that:

collection
  .find(
     Json.obj("relateds" -> Json.obj("$elemMatch" -> activityRelated)),
     Json.obj("subscriberId" -> 1, "_id" -> 0)
  )
  .cursor[JsValue]
  .collect[Seq]() 
  .map(_.map(_.\("subscriberId").as[BSONObjectID]))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top