I am working on writing aggregate queries using java for mongo db for first time. I am not able to convert the shell query which I wrote previously to java format. I am facing some issues. The below is the shell query which I wrote already and it's working fine.
Date set for rooms.
{
"_id": ObjectId("571c5724db62826826d28d08"),
"conversationId": "6puebew70kke29",
"userId": "600",
"firstName": "Test",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.753Z"),
"__v": 0
}
{
"_id": ObjectId("571c5724db62826826d28d09"),
"conversationId": "6puebew70kke29",
"userId": "900",
"firstName": "User",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:18:28.754Z"),
"__v": 0
}
{
"_id": ObjectId("571c574edb62826826d28d0b"),
"conversationId": "fsny11z742kpgb9",
"userId": "600",
"firstName": "FitTest",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.192Z"),
"__v": 0
}
{
"_id": ObjectId("571c574edb62826826d28d0c"),
"conversationId": "fsny11z742kpgb9",
"userId": "800",
"firstName": "Dev",
"profileImagePath": "",
"created": ISODate("2016-04-24T05:19:10.193Z"),
"__v": 0
}
rooms.aggregate([{
$match: {
type: 'PRIVATE'
}
}, {
$group: {
_id: '$conversationId',
users: {
$push: '$userId'
}
}
}, {
$match: {
users: {
$all: [friendProfileData.id, userprofileData.id]
}
}
}, ]
Java code for the above query.
Aggregation agg = newAggregation(
match(Criteria.where("type").is("PRIVATE")),
group("_id", "conversationId"),
group("users").push("userId").as("users")
);
AggregationResults<Rooms> groupResults = mongoOps.aggregate(agg, "rooms", Rooms.class);
List<Rooms> result = groupResults.getMappedResults();
Not able to complete it fully still I am not aware how to write few expressions. Your help is appreciated.