driver-legacy/src/main/com/mongodb/DefaultDBEncoder.java
protected void putDBRef(final String name, final DBRef ref) {
        BasicDBObject dbRefDocument = new BasicDBObject("$ref", ref.getCollectionName()).append("$id", ref.getId());
        if (ref.getDatabaseName() != null) {
            dbRefDocument.put("$db", ref.getDatabaseName());
        }

        putObject(name, dbRefDocument);
    }
protected void putDBRef(final String name, final DBRef ref) {
        BasicDBObject dbRefDocument = new BasicDBObject("$ref", ref.getCollectionName()).append("$id", ref.getId());
        if (ref.getDatabaseName() != null) {
            dbRefDocument.put("$db", ref.getDatabaseName());
        }

        putObject(name, dbRefDocument);
    }
Deals with encoding database references.

@param name the name of the field in the document
@param ref  the database reference object