Mongojack possible to persist id as string

300 Views Asked by At

Is is possible to store the id field of type String in a POJO as a String in a mongo collection?

I've tried to annotate the POJO as described in the official documentation, but the _id collection property is always persisted as an ObjectId.

Here's my very simple POJO

public class Sticker
{
  @Id
  @JsonProperty("_id")
  private String id;

  private String name;


  public String getId()
  {
    return id;
  }

  public void setId( String id )
  {
    this.id = id;
  }

  public String getName()
  {
    return name;
  }

  public void setName( String name)
  {
    this.name= name;
  }
}
1

There are 1 best solutions below

0
On

It depends whether you set the id before storing an instance in the database or not.

If you set it to an arbitrary string, it will be a string in the database. If you don't set it, it will be an ObjectId in the database, because Mongo DB creates an id for you.