简体   繁体   English

MongoDB 返回字段为 null

[英]MongoDB returning field as null

I am currently working on an application connected to a MongoDB instance.我目前正在开发连接到 MongoDB 实例的应用程序。 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.我遇到问题,我的 object 的“id”字段没有在应用程序中返回给我,而是作为 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:我正在使用 Java 同步驱动程序 (4.4.1) 查询集合,如下所示:

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.出于某种原因,当我查询 entity_id 除外时,这将给我 object 中的每个字段。 I keep getting this returned as:我一直把它退回为:

entity_id= null 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.第一个是每个其他字段都是一个字符串(最初 Id 是一个 UUID object 但我在故障排除时进行了简化)并且如果它是其他字段,它们仍然会返回。 The second being that there is a whitespace before this null value as if it's being formatted.第二个是在这个 null 值之前有一个空格,就好像它正在被格式化一样。 Other null values return as field=null instead of field= null其他 null 值返回为 field=null 而不是 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.我想看看是否有一些安全设置阻止事物被标记为 *_id 或 *id 被返回,但我没有发现这样的实例。

Edit: Here is the Entity Pojo for clarity编辑:为清楚起见,这是 Entity Pojo

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;
}

} }

So update for anyone watching this, it appears to have been an issue with my Eclipse IDE.所以更新任何观看此内容的人,这似乎是我的 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.我将项目重新导入到 IntelliJ Community Edition 并重建了 Maven 项目等......这样做之后,测试用例通过了,我的 entityID 在查询中返回。 Hopefully if anyone else runs into this issue they can do something similar.希望如果其他人遇到这个问题,他们可以做类似的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM