使用mongoOperations的Spring数据Mongodb-使用查询条件获取嵌套/嵌入式数组文档

时间:2018-08-03 02:48:07

标签: spring mongodb spring-data spring-data-mongodb

**Using positional projection:**

条件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"
                    }
                ]
            }
}

0 个答案:

没有答案