getMappedResults()没有正确映射原始结果

时间:2017-05-17 15:51:42

标签: mongodb aggregation-framework spring-data-mongodb

在聚合中使用展开操作时,我遇到映射结果的问题。

以下是我的依赖项。在依赖项下面我提到了两个模型类,我在最后使用聚合代码。我在本机查询以及Spring查询中获取结果中的数据。但不同的是,Spring查询结果即使有文件,orgMap数组也是空的。而本机查询返回的文档对orgMap数组中的每个文档都有一个数据。

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <!-- mongodb java driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- Spring data mongodb -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.10.3.RELEASE</version>
        </dependency>





        package com.mongodb.model.user;

        import java.util.HashSet;
        import org.springframework.data.annotation.Id;
        import org.springframework.data.annotation.PersistenceConstructor;
        import org.springframework.data.mongodb.core.mapping.Document;
        import com.alta.mongodb.model.user.UserOrgMap;

        @Document(collection = "users")

        public class Users {

            public Users() {
                super();
            }

            @PersistenceConstructor
            public Users(String userId, String login, String firstName, String lastName, 
                    HashSet<UserOrg> userOrg) {
                super();
                this.userId = userId;
                this.login = login;
                this.firstName = firstName;
                this.lastName = lastName;
                this.userOrgMap = userOrgMap;
            }

            @Id
            private String userId;
            private String login;
            private String firstName;
            private String lastName;
            private HashSet<UserOrgMap> userOrgMap;


            public String getUserId() {
                return userId;
            }
            public void setUserId(String userId) {
                this.userId = userId;
            }
            public String getLogin() {
                return login;
            }
            public void setLogin(String login) {
                this.login = login;
            }
            public String getFirstName() {
                return firstName;
            }
            public void setFirstName(String firstName) {
                this.firstName = firstName;
            }
            public String getLastName() {
                return lastName;
            }
            public void setLastName(String lastName) {
                this.lastName = lastName;
            }

            public HashSet<UserOrgMap> getUserOrgMap() {
                return userOrgMap;
            }
            public void setUserOrgMap(HashSet<UserOrgMap> userOrgMap) {
                this.userOrgMap = userOrgMap;
            }

            @Override
            public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
                result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
                result = prime * result + ((login == null) ? 0 : login.hashCode());
                result = prime * result + ((userId == null) ? 0 : userId.hashCode());
                result = prime * result + ((userOrgMap == null) ? 0 : userOrgMap.hashCode());
                return result;
            }

            @Override
            public boolean equals(Object obj) {
                if (this == obj)
                    return true;
                if (obj == null)
                    return false;
                if (getClass() != obj.getClass())
                    return false;
                Users other = (Users) obj;
                if (firstName == null) {
                    if (other.firstName != null)
                        return false;
                } else if (!firstName.equals(other.firstName))
                    return false;
                if (lastName == null) {
                    if (other.lastName != null)
                        return false;
                } else if (!lastName.equals(other.lastName))
                    return false;
                if (login == null) {
                    if (other.login != null)
                        return false;
                } else if (!login.equals(other.login))
                    return false;
                if (userId == null) {
                    if (other.userId != null)
                        return false;
                } else if (!userId.equals(other.userId))
                    return false;
                if (userOrgMap == null) {
                    if (other.userOrgMap != null)
                        return false;
                } else if (!userOrgMap.equals(other.userOrgMap))
                    return false;
                return true;
            }

            @Override
            public String toString() {
                return "Users [userId=" + userId + ", login=" + login + ", firstName=" + firstName + ", lastName=" + lastName
                        + ", userOrgMap=" + userOrgMap + "]";
            }


        }

    package com.mongodb.model.user;

    import org.springframework.data.annotation.PersistenceConstructor;

    public class UserOrg {



        private String orgId;
        private String createdbyuser;

        public UserOrg() {
            super();
        }

        public UserOrg(String orgId, String createdbyuser) {
            super();
            this.orgId = orgId;
            this.createdbyuser = createdbyuser;
        }

        public String getOrgId() {
            return orgId;
        }

        public void setOrgId(String orgId) {
            this.orgId = orgId;
        }

        public String getCreatedbyuser() {
            return createdbyuser;
        }

        public void setCreatedbyuser(String createdbyuser) {
            this.createdbyuser = createdbyuser;
        }



        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((createdbyuser == null) ? 0 : createdbyuser.hashCode());
            result = prime * result + ((orgId == null) ? 0 : orgId.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            UserOrg other = (UserOrg) obj;
            if (createdbyuser == null) {
                if (other.createdbyuser != null)
                    return false;
            } else if (!createdbyuser.equals(other.createdbyuser))
                return false;
            if (orgId == null) {
                if (other.orgId != null)
                    return false;
            } else if (!orgId.equals(other.orgId))
                return false;

            return true;
        }

        @Override
        public String toString() {
            return "UserOrg [orgId=" + orgId + ", createdbyuser=" + createdbyuser + "]";
        }




    }

 Aggregation aggregation = newAggregation(
                 unwind("userOrgMap")
            );


        AggregationResults<User> groupResults = mongoOperation.aggregate(aggregation,"users", User.class);

        System.out.println(" groupResults raw result "+groupResults.getRawResults().get("result"));

        List<User> result = groupResults.getMappedResults();

System.out.println(" result "+result);


Output : 
groupResults raw result 

[ { "_id" : "user_1" , "login" : "123" , "firstName" : "abc" , "lastName" : "xyz" ,  "userOrgMap" : { "orgId" : "org_1" , "createdbyuser" : "user_2" } , 
  { "_id" : "user_2" ,  "login" : "789" , "firstName" : "qqq" , "lastName" : "aaa" , "userOrgMap" : { "orgId" : "org_1" , "createdbyuser" : "user_3" } 
 ]



 result 

 [
  User [userId=user_1, login=123, firstName=abc, lastName=xyz, userOrgMap=[] ], 
  User [userId=user_2, login=789, firstName=qqq, lastName=aaa, userOrgMap=[],]
 ]


I want to know why userOrgMap array is empty instead of having value similar to the output shown for raw results. 

1 个答案:

答案 0 :(得分:0)

userOrgMap不是原始结果中的数组,而只是单个文档。

你必须有两个选择。

userOrgMap模型中添加额外字段userOrgMapSingle User,并在汇总查询中将unwind别名添加为userOrgMapSingle

添加project阶段,将单个文档转换为具有单个文档的数组,以将其映射到现有的userOrgMap集合类型。

这样的东西
{$project:{userOrgMap:["$userOrgMap"]}}

Spring Mongo Data Equivalent

Aggregation aggregation = newAggregation(
            unwind("userOrgMap"),
            new AggregationOperation() {
               @Override
              public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
              return new BasicDBObject("$addFields", new BasicDBObject("userOrgMap", Arrays.asList("$userOrgMap")));
          }
      }
 );