为什么spring-data-MongoDB支持不是用数据获取所有嵌套的子记录?

时间:2018-04-11 13:09:08

标签: mongodb spring-data-mongodb

我有MongoDB Collection,如关注

MongoDB JSON:

 {
        "_id" : "1",
        "_class" : "com.AColl",
        "BColDate" : {
            "BName" : "xx",
            "BFirstName" : "97908",
            "BList" : []
        },
        "CCollList" : [ 
            {
                "CId" : "1",
                "Cname" : "abc"
                "CList" : [ 
                    {
                        "S" : "N",
                        "Value1" : "",
                        "Val2" : "qq",
                        "Val3" : "ww1"
                    }
                ],
                "addres1" : "",
                "status" : false
            }, 
            {
                "CId" : "2",
                "Cname" : "abc"
                "CList" : [ 
                    {
                        "S" : "N",
                        "Value1" : "",
                        "Val2" : "qq",
                        "Val3" : "ww1"
                    }
                ],
                "addres1" : "",
                "status" : false
            }, 
            {
                "CId" : "3",
                "Cname" : "abc"
                "CList" : [ 
                    {
                        "S" : "N",
                        "Value1" : "",
                        "Val2" : "qq",
                        "Val3" : "ww1"
                    }
                ],
                "addres1" : "",
                "status" : false
            }
        ]
    }

Java PoJo:

 @Document(collection="test")
    class Test implements Serializable{
        private static final long serialVersionUID = 1L;
        @Id
        @Indexed
        private String id;
        @Field("bbcoll")
        private BColl bbcoll;
        @Field("CCollList")
        private List<CCollList> CCollList;
    }

    class BColl implements Serializable {
        private String BName;
        private String BFirstName;
        private List<BList> bList;
    }

    class CCollList implements Serializable {
        private String CId;
        private String CName;
        private List<CList> bList;
    }

    class CList implements Serializable {
        private String S;
        private String Value1;
        private String Value2;
        private String Value3;
    }

Java代码 - Spring Data MongoDB查询:

Test col = mongoOperation.findOne(query, Test.class);

查询回复

Spring数据查询执行响应如下,

{
 "_id" : "1",
 "_class" : "com.AColl",
 "BColDate" :
 { "BName" : "xx", "BFirstName" : "97908", "BList" : [] },
 "CCollList" : [ 
 {
 "CId" : "1",
 "Cname" : "abc"
 "CList" : [
 { "S" : "N", "Value1" : "", "Val2" : "qq", "Val3" : "ww1" }
 ],
 "addres1" : "",
 "status" : false
 }, 
 {
 "CId" : "2",
 "Cname" : "abc"
 "CList" : [
 { "S" : null, "Value1" : null, "Val2" : null, "Val3" : null }
 ],
 "addres1" : "",
 "status" : false
 }, 
 {
 "CId" : "3",
 "Cname" : "abc"
 "CList" : [
 { "S" : null, "Value1" : null, "Val2" : null, "Val3" : null }
 ],
 "addres1" : "",
 "status" : false
 }
 ]
}

请参阅 CList 回复。我能够一次收到一个有效的响应()其他CList部分值被加载为空值,但在MongoDB中,我有此部分的值。为什么会出现这种错误?这个问题是我最近在生产和测试环境中发现的,因为这个问题现在我们正在考虑替代解决方案,而不是仅仅依赖于mongo数据。

问题:

当我尝试迭代“Cid”= 2/3/1的CList对象值并且MongoDB集合列表值为null但是在MongoDB中,值存在(根据json)并且列表对象不为null。

为什么在spring-date-mongoDB(版本1.9)中出现此问题? 否则,请让我知道此问题早先确定了吗?

我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

在阅读完问题之后,您似乎无法以与商店值匹配的方式命名您的java类和属性。请使用@Field注释将属性名称与商店中使用的名称对齐。

在课程bList中说明属性CCollList应该在商店中以CList命名。

"CCollList" : [ 
        {
            "CId" : "1",
            "Cname" : "abc"
            "CList" : [ 

除此之外,请尝试坚持语言特定的约定,使代码更具可读性,并帮助其他人更快地理解它。