Alexander Chepurnoy
Alexander Chepurnoy

Reputation: 615

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 ?

Upvotes: 1

Views: 854

Answers (2)

breinero
breinero

Reputation: 412

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.

Upvotes: 2

breinero
breinero

Reputation: 412

The DBRef's DB object is stored as a private member of DBRef's parent class, DBRefBase, and is accessible with getDB(). You just need to call ref.getDB()

Upvotes: 0

Related Questions