5

In MongoDB documentation they suggest to use ObjecId for manual references. please see https://docs.mongodb.com/manual/reference/database-references/#document-references

original_id = ObjectId()

db.places.insert({
    "_id": original_id,
    "name": "Broadway Center",
    "url": "bc.example.net"
})

db.people.insert({
    "name": "Erin",
    "places_id": original_id,
    "url":  "bc.example.net/Erin"
})

I'm using spring-data-mongodb and what I'm looking for is to have a People class defined like this:

@Document
public class People {

    private String name;
    @Reference // or any Annotation to convert an ObjectId to a String
    private String placesId; 
    private String url;
}

How to have a "places_id" as ObjectId in mongoDB but mapped to a String in our POJO ?

I was expecting to have an annotation like @Reference but it seems to not be implemented.

I don't understand why we don't have this kind of annotation in spring-data-mongodb. I don't want to implement an explicit converter like suggested in spring documentation for all documents that use manual references. Maybe it's not the right approach.

Did I miss something ?

UPDATE :

I like the idea to have a POJO using only String instead of ObjectId. Let's say I've got a class Place like this :

@Document
public class Place {
  @Id
  private String id;
  private String name;
}

place.getId() will be a String but people.getPlaceId() will be an ObjectId. I want to avoid this unnecessary mapping.

4 Answers 4

8

The solution would be:

import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;

public class People {

    @Field(targetType = FieldType.OBJECT_ID)
    private String placesId; 
}

This will map POJO string to ObjectId in MongoDB.

Sign up to request clarification or add additional context in comments.

4 Comments

What can be done if there is List<String> instead of String? I mean list of object Ids.
Good question. I would probably raise a ticket for Spring Data MongoDB GitHub project, because somebody would ask for Set instead of List and so on.
@dma_k Any solution for List<String> ? I mean it's only 3 months but still gotta ask.
I don't have any solution, you'd better ask Spring Data community. Probably one can hack by implementing a custom converter?
3

If you want to make a real reference to an other object in your database, use the @DBRef annotation which is provided by Spring Data.

Your updated code could look like the following:

@Document
public class People {

    private String name;

    @DBRef
    private Place place; 
    private String url;
}

Spring Data will then automatically map a Place object to your People object. Internally this is done with a reference to the unique ObjectId. Try this code and have a look at your mongo database.

For more information have a look at: MongoDb with java foreign key

Comments

3

I have a solution very simple:

@JsonSerialize(using= ToStringSerializer.class)
private ObjectId brandId;
...

put that on the attribute that is Object Id, and the ObjectId gets and inserts like string

1 Comment

The only actually correct solution
2

Why don't you leave the field as ObjectId?

@Document
public class People {

    private String name;
    private ObjectId placesId; 
    private String url;
}

If you want to query by this field you can do this:

  • For lists

    List<String> ids // the ids as strings
    List<ObjectId> objIds = ids .stream()
                                .map(i -> new ObjectId(i))
                                .collect(Collectors.toList());
    
  • For single String

    String id // single id
    ObjectId objId = new ObjectId(id);
    

3 Comments

Thanks Alex. I've updated my question with more details because I would like to avoid this mapping. But what you're suggesting is working.
Unfortunately there is no specific annotation provided yet. Only field _id is handled in the mapping layer. When using the MappingMongoConverter there are certain rules that govern how properties from the Java class is mapped to this _id field
But you could look into customizing the MappingMongoConverter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.