0

I use Mongo DBRef with Java driver, but the Mongo client returns a DBRef object instead of a Map object.

Document doc = new Document();
doc.append("projectName", "New project");
doc.append("owner", new DBRef("users", owner.get_id()));
client.getDatabase("db").getCollection("projects").insertOne(doc);

Then I read the collection

MongoCollection<Document> coll = client.getDatabase("db").getCollection("projects");
FindIterable<Document> result = coll.find(query);

BasicDBList list = new BasicDBList();
result.into(list);

return list;

Thank you for your help

hadf
  • 279
  • 2
  • 5
  • 15
  • 2
    You really don't want that. No new project should be using `DBRef`. It may not have been "officially" deprecated from driver support, however no new feature introduced into MongoDB over the last 5 years actually supports it. Instead, simply use `ObjectID` values and define "relationships" within the scope of your "client side code". This is not up to MongoDB to enforce, and `DBRef` never actually did that anyway. Your "modern" tool for obtaining data from "related" collections is [`$lookup`](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) – Neil Lunn May 06 '18 at 02:07
  • Thank you for your response, I'll have a look to $lookup – hadf May 06 '18 at 12:36

0 Answers0