Search code examples
javamongodbmongo-java

MongoDb + Java: no $db stored with DbRef?


Using mongo-2.8.0.jar, I want to get db-reference to an another db. But code (Groovy)

def ref = new DBRef(db,"mails",id)
println ref

Prints something like:

{ "$ref" : "mails", "$id" : "50211d8e44ae1f34b4f4b3bd" }

And no $db stored in database too!

How to force Java Mongo driver to save $db ?


Solution

  • The Java driver does not support the optional $db field, as described in the MongoDB documentation. Not all drivers support the $db field. The Java driver assumes that the collection is in the same database as the document that is storing the reference.