Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Mongo DB @DBREF

I am having trouble writing code that would allow get a user and claim details in a straightforward way. This is my MongoDB structure,

db.user.find();
user: 
    {
    "name" : "KSK", 
     "claim"  : [objectId("52ffc4a5d85242602e000000"),objectId("52ffc4a5d85242602e000001")] 
    }

claim: 

    [
       {
         "_id" : "52ffc4a5d85242602e000001",
         "claimName" :"XXXX"
       },
       {
         "_id" : "52ffc4a5d85242602e000000",
         "claimName" :"YYY"
       }
    ]

My Entity class is:

@Document(collection="user")
public  class User{
    @Id      
    private String id;
    private String name; 
    @DBRef
    private List<Claim> claim; 
    // setter and getter   
}

Claim Class:

@Document(collection="Claim")
public class Claim{
    @Id 
    private String id; 
    private String claimName;   
}

I have a method to get the users by name like below,

public User findByName(String name);

If I try to hit this method am getting an error that,

No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.String

So I changed my User entity class as like below,

Instead of private List<Claim> claim;

Changed as Private List<ObjectId> claim;

Now if I execute a method(findByName), I get a user object that has both claimed object ids ("52ffc4a5d85242602e000001","52ffc4a5d85242602e000000"), then iterate the claim list and get the claim details corresponding to the claim object Id.

Instead of doing this, when I execute findByName method I want to get a user and claim details. How can I achieve this functionality?

like image 674
KSK Avatar asked May 30 '17 10:05

KSK


People also ask

What is @DBRef in MongoDB?

DBRefs are references from one document to another using the value of the first document's _id field, collection name, and, optionally, its database name, as well as any other fields. DBRefs allow you to more easily reference documents stored in multiple collections or databases.

What is @DBRef in spring boot?

DBRef is MongoDB's native element to express references to other documents with an explicit format { $db : …, $ref : …, $id : … } that holds information about the target database, collection, and id value of the references element, best suited to link to documents distributed across different collections.

How can you implement 1 to many relationships in MongoDB?

In MongoDB, one-to-one, one-to-many, and many-to-many relations can be implemented in two ways: Using embedded documents. Using the reference of documents of another collection.

How do I join two MongoDB collections in spring boot?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.


1 Answers

If you reference your Claims in the User class with @DBRef, your JSON should not only contain the ID but the reference to the collection where to find the ID as well, like this:

{
  "name" : "KSK", 
  "claim" : [ 
     { 
       "$ref" : "claim", // the target collection
       "$id" : ObjectId("52ffc4a5d85242602e000000")
     }
  ] 
}

That is how Spring-Data maps your Java objects to MongoDB. If you start with a blank database and let Spring create and save the relations, you should have no problems using

 @DBRef List<Claim> claims;
like image 70
Jan B. Avatar answered Oct 04 '22 07:10

Jan B.