首页 > 解决方案 > Spring data Mongodb using mongoOperations - 使用查询条件获取嵌套/嵌入数组文档

问题描述

**Using positional projection:**

Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").elemMatch(Criteria.where("emailAddress").is("testuser1@test.com").and("password").is("test1@123" )); 查询查询 = Query.query(elementMatchCriteria); query.fields().position("organizationInformation.users", 1); 列出 customerInfo =mongoOperations.find(query, CustomerInformation.class);

i have tried this approach  in my case have more than one match record found but using positional projection it always giving only one matching record..
please help me is there any way to get multiple matching records using positional or any criteria to get nested/embeded array documents.thanks in advance. 
sample collection as below based emailaddresss and password i should get 2 records but it is always giving one record.

{     "_id" : ObjectId("58f5e68c8205281d68bbb290"), 
            "_class" : "com.test.dataservices.entity.CustomerInformation", 
            "organizationInformation" : {
                "_id" : "123", 
                "companyName" : "Test1", 
                "ibanNumber" : "12345e", 
                "address" : "estates", 
                "contractInformation" : {
                    "duration" : NumberInt(0), 
                    "contractType" : "Gold", 
                    "totalUsers" : NumberInt(0)
                }, 
                "users" : [
                    {

                        "firstName" : "testuser1", 
                        "emailAddress" : "testuser1@test.com", 
                        "password" : "test1@123", 
                        "userAccessType" : "admin"
                    }, 
                    {

                        "firstName" : "testuser1", 
                        "emailAddress" : "testuser1@test.com", 
                        "password" : "test1@123", 
                        "userAccessType" : "IT"
                    }, 
                    {

                        "firstName" : "testuser2", 
                        "emailAddress" : "testuser2@test.com", 
                        "password" : "test2@123", 
                        "userAccessType" : "user"
                    }
                ]
            }
}

标签: springmongodbspring-dataspring-data-mongodb

解决方案


推荐阅读