0

I am currently working on an application connected to a MongoDB instance. I am having trouble where the 'id' field of my object is not being returned to me within the application but is being returned as null.

The schema has an 'entity' as defined below:

{
        "entity_id": String,
        "parent": String,
        "relevance": boolean
}

I'm querying the collection using the Java Sync Driver (4.4.1) like so:

try {
    Entity testDoc = collection.find(eq("entity_id", entity_id)).first(); 
    if (testDoc != null) {
        //add entity to a list
    }
} catch (Exception e) {
    LOGGER.log(Level.SEVERE, "Failed to get Entity", e);
}

For some reason this will give me every field in the object when I query EXCEPT the entity_id. I keep getting this returned as:

entity_id= null

Two things stick out to me. The first being that every other field is a String (originally the Id was a UUID object but I simplified while troubleshooting) and they still return if it's other fields. The second being that there is a whitespace before this null value as if it's being formatted. Other null values return as field=null instead of field= null

I was looking to see if there is some security setting preventing things from being labeled as *_id or *id from being returned but I have found no such instance.

Edit: Here is the Entity Pojo for clarity

public class Entity {

@BsonProperty(value = "entity_id")
private String entityID;
@BsonProperty(value = "parent")
private String parent;
@Deprecated
@BsonProperty(value = "relevance")
private boolean relevance;

public Entity() {}

public Entity(String entityID, String parent, Boolean relevance) {
    this.entityID = entityID;
    this.parent = parent;
    this.relevance = relevance;
}

public String getEntityID() {
    return entityID;
}

public void setEntityID(String entityID) {
    this.entityID = entityID;
}

public String getParent() {
    return parent;
}

public void setParent(String parent) {
    this.parent = parent;
}

public boolean isRelevant() {
    return relevance;
}

public void relevance(boolean relevance) {
    this.relevance = relevance;
}

}

6
  • Did you check what is the value stored in the mongoDB for entity_id ? you can use the MongoDB compass app to access the DB through friendly UI. Maybe you stored the value as UUID and trying to retrieve it as String ? Commented Feb 7, 2022 at 22:56
  • What is the Entity POJO class you are mapping the MongoDB document to? Commented Feb 8, 2022 at 5:25
  • @OrrBenyamini it's stored as String in the DB actually, Commented Feb 8, 2022 at 16:40
  • @prasad_ The entity pojo is a builder pattern, I'll copy it into the main post under an edit Commented Feb 8, 2022 at 16:40
  • It is unconventional to use field names in Java like entityID and have a get method like getID() for that field. A field name like entityId and a get method for it as getEntityId sounds more appropriate. Commented Feb 8, 2022 at 17:06

1 Answer 1

1

So update for anyone watching this, it appears to have been an issue with my Eclipse IDE.

I reimported the project into IntelliJ Community Edition and rebuilt the Maven project, etc... After doing so, the test cases passed and my entityID returns in the query. Hopefully if anyone else runs into this issue they can do something similar.

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

Comments

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.