I am trying to use the following find query,
{
"uid" : { "$in" : ["value1", "value2"] }
}
The array ["value1", "value2"] should be set dynamically from Scala Array[String].
Here is my code,
def find(uids: Array[String]): Future[Seq[User]] = {
val query: JsObject = Json.parse(JsonUtil.toInQuery(uids, "uid")).asInstanceOf[JsObject]
collection.flatMap(_.find(query)
.cursor[User](ReadPreference.primary)
.collect[Seq]()
)
}
The method JsonUtil.toInQuery(uids, "uid") is creating the json string of the query. I have tried using BSONDocument and writing Json query manually instead of passing json string, but it does not seem to work.
Can anyone suggest me a working way to use that query in reactive-mongo with play framework 2.6 ?
Update
public static String toJsonString(String[] arr){
return Json.toJson(arr).toString();
}
public static String toInQuery(String[] arr, String item){
return "{ \""+item+"\": {\"$in\": "+toJsonString(arr)+"}}";
}
I got it working. But, not sure if this is the correct approach.