javamongodbdbref

Mongo Document with DBRef


There are many example how work with mongo document Creating BSON object from JSON string

And other But I can not find how to create a document with a link (DBRef) to another

Something like this

private static final MessageFormat USER_DBREF = new MessageFormat("DBRef('organizationEntity', ObjectId('{}'))");
...
Document doc = Document.parse(json);
doc.put("organizationEntity", USER_DBREF.format(new Object[]{organizationEntity.getId()}));

Solution

  •    doc.put("organization", new DBRef("organizationEntity", new ObjectId(organizationEntity.getId())));
    

    http://api.mongodb.com/java/2.14/com/mongodb/DBRef.html