Raju Talisetty
Raju Talisetty

Reputation: 50

Spring data-Mongo DB Query embedded array

I have the document in mongo collection called (CustomerInformation) with following structure.

        {     "_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" : "[email protected]", 
                        "password" : "test1@123", 
                        "userAccessType" : "admin"
                    }, 
                    {

                        "firstName" : "testuser2", 
                        "emailAddress" : "[email protected]", 
                        "password" : "test2@123", 
                        "userAccessType" : "user"
                    }
                ]
            }
        }

Now i want retrieve only the user information with matching emailAddress and Password. I am trying as follows.

Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").
    elemMatch(Criteria.where("emailaddress").is("[email protected]").and("password").is(test1@123));

    BasicQuery query = new BasicQuery(elementMatchCriteria.getCriteriaObject());

  CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class);

I am getting the complete document with all users array , i want to retrieve only matching user information emailAddress and password. Whats the wrong in my Query or data model? Any suggestions? Thank you!

Upvotes: 2

Views: 4200

Answers (2)

s7vr
s7vr

Reputation: 75934

Use positional projection.

Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").elemMatch(Criteria.where("emailAddress").is("[email protected]").and("password").is("test1@123"));
Query query = Query.query(elementMatchCriteria);
query.fields().position("organizationInformation.users", 1);
CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class);

Upvotes: 2

AshokGK
AshokGK

Reputation: 875

You can use aggregation query with $unwind to achieve this

db.collection.aggregate([
    {
        $unwind:"$organizationInformation.users"
    },
    {
        $match:{
            "organizationInformation.users.emailAddress":"[email protected]",
            "organizationInformation.users.password":"test1@123"
        }
    },
    {
        $project:{
            "organizationInformation.users":1
        }
    }
 ])

Result is:

{
    "_id" : ObjectId("58f5e68c8205281d68bbb290"),
    "organizationInformation" : {
        "users" : {
            "firstName" : "testuser1",
            "emailAddress" : "[email protected]",
            "password" : "test1@123",
            "userAccessType" : "admin"
        }
    }
}

OR

db.collection.aggregate([
    {
        $unwind:"$organizationInformation.users"
    },
    {
        $match:{
            "organizationInformation.users.emailAddress":"[email protected]",
            "organizationInformation.users.password":"test1@123"
        }
    },
    {
        $project:{
            user: "$organizationInformation.users"
        }
    }
])

Result is:

{
    "_id" : ObjectId("58f5e68c8205281d68bbb290"),
    "user" : {
        "firstName" : "testuser1",
        "emailAddress" : "[email protected]",
        "password" : "test1@123",
        "userAccessType" : "admin"
    }
}

Upvotes: 0

Related Questions