How to implement the following Mongo DB query using Spring Data?
db.getCollection('docs').aggregate([
{
$project: {
values: [ "$a", "$b" ]
}
}
])
AggregationOperation projectStage = Aggregation.project()
.andArrayOf("$a","$b").as("values");
Related
I need help with Spring Data Java API. I am writing aggregation pipeline for the report. The last stage is "$project" where I set the data for output. In Mongodb shell the pipeline works just fine but I can't find the way to code "generatedDate: new Date()" with Spring Data SDK for MongoDB.
Here is how the "$project" should look like:
{$project: {
coach: {
firstName: '$coach.firstName',
lastName: '$coach.lastName',
employeeId: '$coach._id'
},
patient: {
firstName: '$patientContact.patient.firstName',
lastName: '$patientContact.patient.lastName',
contactId: '$patientContact._id'
},
customerName: '$patientContact.organization.name',
coachingSummary: 1,
formCreatedDate: 1,
generatedDate: new Date() //<<--- This is what I want
}}
I want the server to generate new Date instance and return it in the response.
Here is my Java code for this aggregation stage:
ProjectionOperation finalProject = project("coachingSummary", "formCreatedDate")
.and("$patientContact.organization.name").as("customerName")
.and("coach")
.nested(Fields.from(
Fields.field("firstName", "coach.firstName"),
Fields.field("lastName", "coach.lastName"),
Fields.field("employeeId", "coach._id")
))
.and("patient")
.nested(Fields.from(
Fields.field("firstName", "patientContact.patient.firstName"),
Fields.field("lastName", "patientContact.patient.lastName"),
Fields.field("contactId", "patientContact._id")
))
.and("<SOMETHING_GOES_HERE>").as("generatedDate");//<<-- How to code it?
i have to find the max value in spring boot with mongoDB. Here is my sample code which i have tried and successfully executed in mongoDB console
db.ecle_audit_transaction.aggregate(
[
{
$group:
{
_id: "$rootId",
logMessageType: { $max: "$logMessageType" }
}
}
]
)
could you please help me how to write this query in spring bootmongoTemplate
i'm looking to reproduce this snipet into Java code :
db.getCollection('admins_comptes_client_ceov4').aggregate([
{$lookup: {from: "contrats_ceov4",localField: "CUSTOMERNUMBER",foreignField: "CUSTOMERNUMBER",as: "arrayForeignObject"}
{$unwind: { path: "$arrayForeignObject", preserveNullAndEmptyArrays: true}},
{$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }},
{ $project: { "arrayForeignObject": 0, "_id": 0 } },
{ $out: "aggregate" }])
I'm here so far :
AggregationOperation lookup = Aggregation.lookup(fromCollection, localField, toMatchWith, "arrayForeignObject");
AggregationOperation unwind = Aggregation.unwind("arrayForeignObject", true);
AggregationOperation replaceRoot = Aggregation.replaceRoot(Aggregation.ROOT);
AggregationOperation project = Aggregation.project("arrayForeignObject").andExclude("_id");
AggregationOperation out = Aggregation.out("aggregate");
Aggregation aggregation = Aggregation.newAggregation(lookup, replaceRoot, unwind, project, out);
mongoTemplate.aggregate(aggregation, initialCollection, AggregateModel.class);
I've got an issue on the following point :
{$replaceRoot: { newRoot: { $mergeObjects: [ "$arrayForeignObject", "$$ROOT" ] } }
I can't succeed to make a mergeObjects.
With this following java snippet, the AggregationOperation outcome is :
"$replaceRoot" : { "newRoot" : "$arrayForeignObject" }
When i execute this java snippet, i end up with the new collection having only the foreign array inside and an _id field.
Does anyone faced this already and could give a hand please?
Frigg0
You have couple of issues here. Use 2.1.0 Release Spring Mongodb jar.
AggregationOperation replaceRoot = Aggregation.replaceRoot().withValueOf(ObjectOperators.valueOf("arrayForeignObject").mergeWith(Aggregation.ROOT));
AggregationOperation project = Aggregation.project().andExclude("_id", "arrayForeignObject");
Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);
For lower versions of spring mongodb
AggregationOperation replaceRoot = ReplaceRootOperation.builder().withDocument("$mergeObjects", Arrays.asList("$arrayForeignObject", Aggregation.ROOT));
AggregationOperation project = Aggregation.project().andExclude("_id", "arrayForeignObject");
Aggregation aggregation = Aggregation.newAggregation(lookup, unwind, replaceRoot, project, out);
db.collection.aggregate([
{$match : { name : "name" } },
{$project: {
name: 1,
sent: {
$size: {
"$ifNull": [ "$audience", [] ]
}
}
}
}]);
How can I do the above mongo aggregation with Spring data?
I know this is an old post and you might have found the answer, but, just for the sake of others, I'm posting it here.
Aggregation aggregation = Aggregation.newAggregation(
.match(Criteria.where("name").is("name"))
.project("name")
.and(ArrayOperators.Size.lengthOfArray(ConditionalOperators.ifNull("audience").then(Collections.emptyList()))).as("sent")
);
I have aggregation that works in mongo and i need to create the exact one in java with spring. I didn't find a way. Do you know if there is one?
db.collection_name.aggregate([
{
$group: {
_id : {
year : {$year : "$receivedDate" },
month : {$month: "$receivedDate"},
day : { $dayOfMonth : "$receivedDate"}
},
count : { $sum: 1 }
}
}
])
You could try projecting the fields first by using the SpEL andExpression in the projection operation and then group by the new fields in the group operation:
Aggregation agg = newAggregation(
project()
.andExpression("year(receivedDate)").as("year")
.andExpression("month(receivedDate)").as("month")
.andExpression("dayOfMonth(receivedDate)").as("day"),
group(fields().and("year").and("month").and("day"))
.count().as("count")
);