2
votes

Following is my MongoDB query which is

{$project:{
       firstName: "$firstName",
       lastName:"$lastName",
       email:"$email",
       company :"$organization.name",
       RoleName :"$organization.roles.roleName",
       matchingRole: { $eq: [ "$organization.roles.orgRoleId","$userOrgMap.roleId" ] }
      }
    },
    { $match: {matchingRole:true},{ $sort : { firstName : 1} }])

Following is my SpringData mongo API

aggregation = newAggregation(
                project("firstName", "lastName", "email")
                        .and("organization.name").as("company")
                        .and("organization.roles.roleName").as("roleName")

I want to know how to use $eq in project aggregate using Spring API. I tried to put the match outside the project as shown below. But query is not fetching mew any records. So I used $eq to compare in the project and assign the result in a new project attribute and check for it outside the project pipeline.

{ $match: {$organization.roles.orgRoleId","$userOrgMap.roleId"}

1
I have come up with the following equivalent project("_id", "login", "firstName", "lastName", "email", "deactivateFlag", "lastActivity") .and("organization.name").as("company") .and("organization.roles.roleName").as("roleName") .and(when(where("organization.roles.orgRoleId").is("userOrgMap.roleId")).then(true).otherwise(false)).as("matchingRoleIds"), match(Criteria.where("matchingRoleIds").is(true)), - karmaker
Need to test this API - karmaker

1 Answers

1
votes

You can use the $eq aggregation the following way in $project stage.

and(ComparisonOperators.Eq.valueOf("organization.roles.orgRoleId").equalTo("us‌​erOrgMap.roleId")).as("matchingRole"),