marcel
marcel

Reputation: 407

ReactiveMongo scala null

An easy one I guess. When executing on the mongo shell:

db.topic.find({"user_id":ObjectId("52e39be16700006700d646ad"), "post_id":null})

It will list all topics where post_id either is null or does not exist. This works fine.

However when using Scala code I try following which doesn't work:

val cursor = db("topic").find(
  BSONDocument("user_id" -> user.id),
  BSONDocument("post_id" -> null)).cursor[Topic]
cursor.collect[List]()

Basically I have to change the condition BSONDocument("post_id" -> null). But how?

Many thanks! Marcel

Upvotes: 1

Views: 1111

Answers (1)

yǝsʞǝla
yǝsʞǝla

Reputation: 16422

In mongo shell query you have one JSON document that defines the query clause, but in Scala code you have 2 JSON documents, so the second document defines projection just like it would in Mongo shell. Here is def find[S, P](selector: S, projection: P)... documentation. You need to make a single document with two fields out of your two docs like this:

val cursor = db("topic").find(
  BSONDocument("user_id" -> user.id, "post_id" -> null)).cursor[Topic]
cursor.collect[List]()

Upvotes: 3

Related Questions