Jessie
Jessie

Reputation: 1105

mongodb dbref java

I am newbie to mpngodb.

I have two collections.

 Tenant
 {
 Tenant_ID:123, Tenant_Info:new
 Tenant_ID:456, Tenant_Info:old
 }

 System
 {
  System_ID:768, Tenant_ID:123,System_Info:"check"
 }

I need to reference the Tenant collection Tenant_ID with System collection Tenant_ID.

Could anyone help me with the DBREF java code for mongodb to achieve this relationship?

Upvotes: 0

Views: 1810

Answers (2)

Shoopman
Shoopman

Reputation: 186

Take a look at Spring Data MongoDB.

Upvotes: 0

Stennie
Stennie

Reputation: 65333

Database References (DBRefs) are used by convention; they do not correspond to a supported feature in the MongoDB server.

If you want to store a reference to another collection in your document you could do it a few ways:

1) Just save the relevant key for the other collection (in your example the Tenant_ID of the related Tenant document wants to be saved as a field in a document in the System collection).

2) Use the DBRef class to construct a reference to the object and then [fetch()](http://api.mongodb.org/java/current/com/mongodb/DBRef.html#fetch(com.mongodb.DB, com.mongodb.DBObject)) the referenced object.

Based on your example, it looks like the first usage would be more relevant - you appear to be saving the Tenant_ID field in your System document. In this case, you can load a System document and use findOne() to retrieve the related Tenant document based on the Tenant_ID (assuming Tenant_ID uniquely identifies a Tenant).

Upvotes: 1

Related Questions