Spring

 View Only

 mongoDb aggregation to Spring data

vishal gupta's profile image
vishal gupta posted Dec 05, 2019 12:59 PM

Could you please help me to write following mongodb aggrigation into the spring data

db.result.aggregate([

{

$addFields: {

gstinobjids: {

$map:

 

{ input: "$gstids", as: "r", in: \{ $toObjectId: "$$r" }

}

}, gstinobjids2: {

$map:

 

{ input: "$erpids", as: "r", in: \{ $toObjectId: "$$r" }

}

}

}

}

,{

$lookup:

 

{ from:"financial_data", localField: "gstinobjids", foreignField: "_id", as: "result" }

},{

$lookup:

 

{ from:"purchase_data", localField: "gstinobjids2", foreignField: "_id", as: "result2" }

}

])

Daniel Mikusa's profile image
Daniel Mikusa

I believe you want the Aggregate Framework Support. See the docs here. In short, you'll use `MongoTemplate.aggregate(..)` and pass in the AggregateOperation you want to run. You can use `Aggregation.newAggregation(..)` to create your custom aggregation.

 

https://docs.spring.io/spring-data/mongodb/docs/2.2.3.RELEASE/reference/html/#mongo.aggregation

 

Hope that helps!

vishal gupta's profile image
vishal gupta

yes I know but the think is that i am not able to convert following part into the spring data or boot. $addFields and $map is not even presnt into the above link.

 

$addFields: {

gstinobjids: {

$map:

 

{ input: "$gstids", as: "r", in: \{ $toObjectId: "$$r" }

}

}

vishal gupta's profile image
vishal gupta

yes I know but the thing is that i am not able to convert following part into the spring data or boot. $addFields and $map is not even presnt into the above link.

 

$addFields: {

gstinobjids: {

$map:

 

{ input: "$gstids", as: "r", in: \{ $toObjectId: "$$r" }

}

}