Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

"_id" field mapped to null || PojoCodecRegistry

Hi,

  1. I am using PojoCodecRegistry (BSON library) and need to map the the "_id " field of the Mongo DB document to user defined POJO field.

  2. However, the “_id” field is getting mapped to null. I have a property named as :

  3. I am using mongo java 4.0 driver and JAVA 8

Hello Aditya,

Can you provide details about how you are mapping the _id field - please include your Pojo class with the _id field mapping.

Also, please include code you had tried to build the Pojo object and insert into the collection.

Thanks So very much for acknowledging the concern.
The POJO class is something like below ::

package org.mongo;

//imports

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Student {
public Student() {
}

@BsonProperty("_id")     
@JsonProperty("_id")
private ObjectId _id;

private String name;
private int age;
private int courseCount;
private String email;
private boolean isVerified;
private ArrayList<String> hobbies;
private ContactDetails contactDetails;
private ArrayList<Marks> marks;

// Getters and Setters

// toString() method

}

Basically this error is occurring when the BSON is deserialized to Java Object. _id field is mapped to null. Rest all other fields are mapped properly.

Please let me know how to solve this problem.

The Pojo class is good.

You can create Pojo objects and insert into a MongoDB collection as documents and retrieve them - without problem. All this using the default driver created ObjectId.

You can post the code related with creating the object (new Student(), setting the properties, etc.,), how you are inserting and retrieving.

Also, see code examples at: MongoDB Java Driver - POJOs.