0

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()}));
LeshaRB
  • 1,345
  • 2
  • 23
  • 44

1 Answers1

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

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

LeshaRB
  • 1,345
  • 2
  • 23
  • 44