Posts

Showing posts from September, 2021

Sample code to write custom query in reactive mongo

 This is a sample code snippet to demostrate how to write a custom aggregation with spring data reactive mongo. Here are the sample collection structures: Collection: UserData {    "userId":"123456",    "profilePhotoAssetId":"abcd1234" } Collection: Comment {    "authorId":"123456",    "body":"This is a sample comment",    "userPhotoId": null } userPhotoId is a transient field. It'll be set from the aggregation result. I want to get all comments and inside the comment, I want the profilePhotoAssetId from UserData in the userPhotoId field.  For the above example, the sample output should be: {    "authorId":"123456",    "body":"This is a sample comment",    "userPhotoId":"abcd1234" } What are the steps? 1. First step is to write a lookup operation that'll join the two collection based on comment.authorId and userData.userId. After