Here is the Scenario, I have 5 tables having one to many relations with each other, I have to map result data in hierarchical manner in below given Pojos with Jooq.
DB Tables are a, b, c, d, e
// Here are response Pojo's
Class APojo {
public string name;
public List<BPojo> listOfB;
}
Class BPojo {
public string name;
public List<CPojo> listOfC;
}
Class CPojo {
public string name;
public List<DPojo> listOfD;
}
Class DPojo {
public string name;
public List<EPojo> listOfE;
}
Class EPojo {
public string name;
}
Expected sample response
{
"name":"A1",
"list_of_b":[
{
"name":"A1B1",
"list_of_c":[
{
"name":"A1B1C1",
"list_of_d":[
{
"name":"A1B1C1D1",
"list_of_e":[
{
"name":"A1B1C1D1E1"
},
{
"name":"A1B1C1D1E2"
}
]
},
{
"name":"A1B1C1D2",
"list_of_e":[
{
"name":"A1B1C1D2E1"
},
{
"name":"A1B1C1D2E2"
}
]
}
]
},
{
"name":"A1B1C2",
"list_of_d":[
{
"name":"A1B1C2D1",
"list_of_e":[
{
"name":"A1B1C2D1E1"
},
{
"name":"A1B1C2D1E2"
}
]
},
{
"name":"A1B1C2D2",
"list_of_e":[
{
"name":"A1B1C2D2E1"
},
{
"name":"A1B1C2D2E2"
}
]
}
]
}
]
},
{
"name":"A1B2",
"list_of_c":[
{
"name":"A1B2C1",
"list_of_d":[
{
"name":"A1B2C1D1",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
},
{
"name":"A1B2C1D2",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
}
]
},
{
"name":"A1B2C2",
"list_of_d":[
{
"name":"A1B2C2D1",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
},
{
"name":"A1B2C2D2",
"list_of_e":[
{
"name":"A1B1C1D1"
},
{
"name":"A1B1C1D2"
}
]
}
]
}
]
}
]
}
I tried something like this first but It did not work because fetch groups only accepts 2 arguments
using(configuration()).select(A.fields())
.select(B.fields())
.select(C.fields())
.select(D.fields())
.select(E.fields())
.from(A)
.join(B).on(A.ID.eq(B.A_ID)
.join(C).on(B.ID.eq(C.B_ID)
.join(D).on(C.ID.eq(D.C_ID)
.join(E).on(D.ID.eq(E.D_ID)
.fetchGroups(
r -> r.into(A).into(APojo.class),
r -> r.into(B).into(BPojo.class),
r -> r.into(C).into(CPojo.class),
r -> r.into(D).into(DPojo.class),
r -> r.into(E).into(EPojo.class)
);
Then I got this post and tried it as given below and other 2 method given in the post, but this also did not worked because Collectors.toMap accepts only 2 arguments and I have to fetch 5 level hierarchical data.
using(configuration()).select(A.fields())
.select(B.fields())
.select(C.fields())
.select(D.fields())
.select(E.fields())
.from(A)
.join(B).on(A.ID.eq(B.A_ID)
.join(C).on(B.ID.eq(C.B_ID)
.join(D).on(C.ID.eq(D.C_ID)
.join(E).on(D.ID.eq(E.D_ID)
.collect(Collectors.groupingBy(
r -> r.into(A).into(APojo.class),
Collectors.toMap(
r -> r.into(B).into(BPojo.class),
r -> r.into(C).into(CPojo.class)
r -> r.into(D).into(DPojo.class)
r -> r.into(E).into(EPojo.class)
)));
The JOIN approach
Historically, most ORMs attempted to nest collections in some way using joins, since joins have been the only widely supported way of "connecting" collections (but not nesting them) in SQL. The result is a flat, denormalised table, that is hard to normalise again. There are a lot of duplicates, and possibly even unwanted cartesian products, and it might not even be possible to be sure what nested collection belongs to a parent value. In your case, it would be possible, but very wasteful both on the server and on the client. The values of A would be repeated many many times.
Some workarounds have been implemented, including by third parties (for jOOQ). Alternatives include running several queries and connecting the values afterwards. All of them very tedious.
Luckily, jOOQ 3.14+ offers support for nesting collections out of the box!
A jOOQ 3.14 approach using SQL/JSON
The jOOQ 3.14 appraoch to nesting collections is using SQL/JSON behind the scenes (or SQL/XML, but in your case, JSON seems more appropriate).
From your question, I don't see why you need the POJO intermediate step, so perhaps you can bypass that and generate the JSON directly in the database. If not, see below.
Write this query:
ctx
.select(
// Optionally, wrap this level in jsonArrayAgg(jsonObject()) too, like the others
A.NAME,
field(
select(jsonArrayAgg(jsonObject(
key("name").value(B.NAME),
key("list_of_c").value(
select(jsonArrayAgg(jsonObject(
key("name").value(C.NAME),
key("list_of_d").value(
select(jsonArrayAgg(jsonObject(
key("name").value(D.NAME),
key("list_of_e").value(
select(jsonArrayAgg(jsonObject(key("name").value(E.NAME))))
.from(E)
.where(E.D_ID.eq(D.ID))
)
)))
.from(D)
.where(D.C_ID.eq(C.ID))
)
)))
.from(C)
.where(C.B_ID.eq(B.ID))
)
)))
.from(B)
.where(B.A_ID.eq(A.ID))
).as("list_of_b")
)
.from(A)
.fetch();
The usual static import is assumed:
import static ord.jooq.impl.DSL.*;
Since jOOQ is all about dynamic SQL, chances are, you can automate some of the nesting with dynamic SQL.
All of the above also works with JSONB in PostgreSQL, just use jsonbArrayAgg() and jsonbObject() instead.
Note that JSON_ARRAYAGG() aggregates empty sets into NULL, not into an empty []. If that's a problem, use COALESCE()
Mapping the above into POJOs
If you have Jackson or Gson on your classpath, you can now just write fetchInto(APojo.class) at the end to map the resulting JSON tree. But you're probably just going to map the POJOs back into JSON again using either Jackson or Gson, so from a high level, I don't think you get a lot of value out of this step.
The jOOQ 3.15 approach to nesting collections
Starting from jOOQ 3.15, a number of improvements to type safe mapping and nesting collections will be implemented
#3884 MULTISET and ARRAY constructor from subquery support (finally!)
#7100 Ad-hoc Field data type conversion convenience
#11804 Type safe mapping of Record[N] types to constructor references
#11812 Support for nested ROW expressions in projections
With all of the above, in case you really need your POJO intermediary step (and assuming you will have the necessary "immutable constructors" on your POJOs, e.g. like canonical record constructors in Java 16+). E.g.
record EPojo (String name) {}
record DPojo (String name, EPojo[] listOfE) {}
record CPojo (String name, DPojo[] listOfD) {}
record BPojo (String name, CPojo[] listOfC) {}
record APojo (String name, BPojo[] listOfB) {}
In that case, you will be able to write something like this:
ctx
.select(A.NAME, array(
select(row(B.NAME, array(
select(row(C.NAME, array(
select(row(D.NAME, array(
select(row(E.NAME).mapping(EPojo::new))
.from(E)
.where(E.D_ID.eq(D.ID))
)).mapping(DPojo::new))
.from(D)
.where(D.C_ID.eq(C.ID))
)).mapping(CPojo::new))
.from(C)
.where(C.B_ID.eq(B.ID))
)).mapping(BPojo::new))
.from(B)
.where(B.A_ID.eq(A.ID))
)
.from(A)
.fetch(Records.mapping(APojo::new));
If you prefer List<SomePojo> over SomePojo[], then you'd just have to use the new ad-hoc conversion from arrays to list on the array expressions, e.g.
array(select(...)).convertFrom(Arrays::asList)
I'll update this part of the answer, once the API has stabilised.
Further outlook
Since these types of "nested collection joins" will become very common in jOOQ, irrespective of whether SQL collections are nested in SQL, or JSON collections, or XML collections, future versions of jOOQ will probably offer a more convenient syntax, similar to the implicit join syntax for ordinary to-one joins.
I have a Mongo document which holds an array of elements.
I'd like to reset the .handled attribute of all objects in the array where .profile = XX.
The document is in the following form:
{
"_id": ObjectId("4d2d8deff4e6c1d71fc29a07"),
"user_id": "714638ba-2e08-2168-2b99-00002f3d43c0",
"events": [{
"handled": 1,
"profile": 10,
"data": "....."
} {
"handled": 1,
"profile": 10,
"data": "....."
} {
"handled": 1,
"profile": 20,
"data": "....."
}
...
]
}
so, I tried the following:
.update({"events.profile":10},{$set:{"events.$.handled":0}},false,true)
However it updates only the first matched array element in each document. (That's the defined behaviour for $ - the positional operator.)
How can I update all matched array elements?
With the release of MongoDB 3.6 ( and available in the development branch from MongoDB 3.5.12 ) you can now update multiple array elements in a single request.
This uses the filtered positional $[<identifier>] update operator syntax introduced in this version:
db.collection.update(
{ "events.profile":10 },
{ "$set": { "events.$[elem].handled": 0 } },
{ "arrayFilters": [{ "elem.profile": 10 }], "multi": true }
)
The "arrayFilters" as passed to the options for .update() or even
.updateOne(), .updateMany(), .findOneAndUpdate() or .bulkWrite() method specifies the conditions to match on the identifier given in the update statement. Any elements that match the condition given will be updated.
Noting that the "multi" as given in the context of the question was used in the expectation that this would "update multiple elements" but this was not and still is not the case. It's usage here applies to "multiple documents" as has always been the case or now otherwise specified as the mandatory setting of .updateMany() in modern API versions.
NOTE Somewhat ironically, since this is specified in the "options" argument for .update() and like methods, the syntax is generally compatible with all recent release driver versions.
However this is not true of the mongo shell, since the way the method is implemented there ( "ironically for backward compatibility" ) the arrayFilters argument is not recognized and removed by an internal method that parses the options in order to deliver "backward compatibility" with prior MongoDB server versions and a "legacy" .update() API call syntax.
So if you want to use the command in the mongo shell or other "shell based" products ( notably Robo 3T ) you need a latest version from either the development branch or production release as of 3.6 or greater.
See also positional all $[] which also updates "multiple array elements" but without applying to specified conditions and applies to all elements in the array where that is the desired action.
Also see Updating a Nested Array with MongoDB for how these new positional operators apply to "nested" array structures, where "arrays are within other arrays".
IMPORTANT - Upgraded installations from previous versions "may" have not enabled MongoDB features, which can also cause statements to fail. You should ensure your upgrade procedure is complete with details such as index upgrades and then run
db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
Or higher version as is applicable to your installed version. i.e "4.0" for version 4 and onwards at present. This enabled such features as the new positional update operators and others. You can also check with:
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
To return the current setting
UPDATE:
As of Mongo version 3.6, this answer is no longer valid as the mentioned issue was fixed and there are ways to achieve this. Please check other answers.
At this moment it is not possible to use the positional operator to update all items in an array. See JIRA http://jira.mongodb.org/browse/SERVER-1243
As a work around you can:
Update each item individually
(events.0.handled events.1.handled
...) or...
Read the document, do the edits
manually and save it replacing the
older one (check "Update if
Current" if you want to ensure
atomic updates)
What worked for me was this:
db.collection.find({ _id: ObjectId('4d2d8deff4e6c1d71fc29a07') })
.forEach(function (doc) {
doc.events.forEach(function (event) {
if (event.profile === 10) {
event.handled=0;
}
});
db.collection.save(doc);
});
I think it's clearer for mongo newbies and anyone familiar with JQuery & friends.
This can also be accomplished with a while loop which checks to see if any documents remain that still have subdocuments that have not been updated. This method preserves the atomicity of your updates (which many of the other solutions here do not).
var query = {
events: {
$elemMatch: {
profile: 10,
handled: { $ne: 0 }
}
}
};
while (db.yourCollection.find(query).count() > 0) {
db.yourCollection.update(
query,
{ $set: { "events.$.handled": 0 } },
{ multi: true }
);
}
The number of times the loop is executed will equal the maximum number of times subdocuments with profile equal to 10 and handled not equal to 0 occur in any of the documents in your collection. So if you have 100 documents in your collection and one of them has three subdocuments that match query and all the other documents have fewer matching subdocuments, the loop will execute three times.
This method avoids the danger of clobbering other data that may be updated by another process while this script executes. It also minimizes the amount of data being transferred between client and server.
This does in fact relate to the long standing issue at http://jira.mongodb.org/browse/SERVER-1243 where there are in fact a number of challenges to a clear syntax that supports "all cases" where mutiple array matches are found. There are in fact methods already in place that "aid" in solutions to this problem, such as Bulk Operations which have been implemented after this original post.
It is still not possible to update more than a single matched array element in a single update statement, so even with a "multi" update all you will ever be able to update is just one mathed element in the array for each document in that single statement.
The best possible solution at present is to find and loop all matched documents and process Bulk updates which will at least allow many operations to be sent in a single request with a singular response. You can optionally use .aggregate() to reduce the array content returned in the search result to just those that match the conditions for the update selection:
db.collection.aggregate([
{ "$match": { "events.handled": 1 } },
{ "$project": {
"events": {
"$setDifference": [
{ "$map": {
"input": "$events",
"as": "event",
"in": {
"$cond": [
{ "$eq": [ "$$event.handled", 1 ] },
"$$el",
false
]
}
}},
[false]
]
}
}}
]).forEach(function(doc) {
doc.events.forEach(function(event) {
bulk.find({ "_id": doc._id, "events.handled": 1 }).updateOne({
"$set": { "events.$.handled": 0 }
});
count++;
if ( count % 1000 == 0 ) {
bulk.execute();
bulk = db.collection.initializeOrderedBulkOp();
}
});
});
if ( count % 1000 != 0 )
bulk.execute();
The .aggregate() portion there will work when there is a "unique" identifier for the array or all content for each element forms a "unique" element itself. This is due to the "set" operator in $setDifference used to filter any false values returned from the $map operation used to process the array for matches.
If your array content does not have unique elements you can try an alternate approach with $redact:
db.collection.aggregate([
{ "$match": { "events.handled": 1 } },
{ "$redact": {
"$cond": {
"if": {
"$eq": [ { "$ifNull": [ "$handled", 1 ] }, 1 ]
},
"then": "$$DESCEND",
"else": "$$PRUNE"
}
}}
])
Where it's limitation is that if "handled" was in fact a field meant to be present at other document levels then you are likely going to get unexepected results, but is fine where that field appears only in one document position and is an equality match.
Future releases ( post 3.1 MongoDB ) as of writing will have a $filter operation that is simpler:
db.collection.aggregate([
{ "$match": { "events.handled": 1 } },
{ "$project": {
"events": {
"$filter": {
"input": "$events",
"as": "event",
"cond": { "$eq": [ "$$event.handled", 1 ] }
}
}
}}
])
And all releases that support .aggregate() can use the following approach with $unwind, but the usage of that operator makes it the least efficient approach due to the array expansion in the pipeline:
db.collection.aggregate([
{ "$match": { "events.handled": 1 } },
{ "$unwind": "$events" },
{ "$match": { "events.handled": 1 } },
{ "$group": {
"_id": "$_id",
"events": { "$push": "$events" }
}}
])
In all cases where the MongoDB version supports a "cursor" from aggregate output, then this is just a matter of choosing an approach and iterating the results with the same block of code shown to process the Bulk update statements. Bulk Operations and "cursors" from aggregate output are introduced in the same version ( MongoDB 2.6 ) and therefore usually work hand in hand for processing.
In even earlier versions then it is probably best to just use .find() to return the cursor, and filter out the execution of statements to just the number of times the array element is matched for the .update() iterations:
db.collection.find({ "events.handled": 1 }).forEach(function(doc){
doc.events.filter(function(event){ return event.handled == 1 }).forEach(function(event){
db.collection.update({ "_id": doc._id },{ "$set": { "events.$.handled": 0 }});
});
});
If you are aboslutely determined to do "multi" updates or deem that to be ultimately more efficient than processing multiple updates for each matched document, then you can always determine the maximum number of possible array matches and just execute a "multi" update that many times, until basically there are no more documents to update.
A valid approach for MongoDB 2.4 and 2.2 versions could also use .aggregate() to find this value:
var result = db.collection.aggregate([
{ "$match": { "events.handled": 1 } },
{ "$unwind": "$events" },
{ "$match": { "events.handled": 1 } },
{ "$group": {
"_id": "$_id",
"count": { "$sum": 1 }
}},
{ "$group": {
"_id": null,
"count": { "$max": "$count" }
}}
]);
var max = result.result[0].count;
while ( max-- ) {
db.collection.update({ "events.handled": 1},{ "$set": { "events.$.handled": 0 }},{ "multi": true })
}
Whatever the case, there are certain things you do not want to do within the update:
Do not "one shot" update the array: Where if you think it might be more efficient to update the whole array content in code and then just $set the whole array in each document. This might seem faster to process, but there is no guarantee that the array content has not changed since it was read and the update is performed. Though $set is still an atomic operator, it will only update the array with what it "thinks" is the correct data, and thus is likely to overwrite any changes occurring between read and write.
Do not calculate index values to update: Where similar to the "one shot" approach you just work out that position 0 and position 2 ( and so on ) are the elements to update and code these in with and eventual statement like:
{ "$set": {
"events.0.handled": 0,
"events.2.handled": 0
}}
Again the problem here is the "presumption" that those index values found when the document was read are the same index values in th array at the time of update. If new items are added to the array in a way that changes the order then those positions are not longer valid and the wrong items are in fact updated.
So until there is a reasonable syntax determined for allowing multiple matched array elements to be processed in single update statement then the basic approach is to either update each matched array element in an indvidual statement ( ideally in Bulk ) or essentially work out the maximum array elements to update or keep updating until no more modified results are returned. At any rate, you should "always" be processing positional $ updates on the matched array element, even if that is only updating one element per statement.
Bulk Operations are in fact the "generalized" solution to processing any operations that work out to be "multiple operations", and since there are more applications for this than merely updating mutiple array elements with the same value, then it has of course been implemented already, and it is presently the best approach to solve this problem.
First: your code did not work because you were using the positional operator $ which only identifies an element to update in an array but does not even explicitly specify its position in the array.
What you need is the filtered positional operator $[<identifier>]. It would update all elements that match an array filter condition.
Solution:
db.collection.update({"events.profile":10}, { $set: { "events.$[elem].handled" : 0 } },
{
multi: true,
arrayFilters: [ { "elem.profile": 10 } ]
})
Visit mongodb doc here
What the code does:
{"events.profile":10} filters your collection and return the documents matching the filter
The $set update operator: modifies matching fields of documents it acts on.
{multi:true} It makes .update() modifies all documents matching the filter hence behaving like updateMany()
{ "events.$[elem].handled" : 0 } and arrayFilters: [ { "elem.profile": 10 } ]
This technique involves the use of the filtered positional array with arrayFilters. the filtered positional array here $[elem] acts as a placeholder for all elements in the array fields that match the conditions specified in the array filter.
Array filters
You can update all elements in MongoDB
db.collectioname.updateOne(
{ "key": /vikas/i },
{ $set: {
"arr.$[].status" : "completed"
} }
)
It will update all the "status" value to "completed" in the "arr" Array
If Only one document
db.collectioname.updateOne(
{ key:"someunique", "arr.key": "myuniq" },
{ $set: {
"arr.$.status" : "completed",
"arr.$.msgs": {
"result" : ""
}
} }
)
But if not one and also you don't want all the documents in the array to update then you need to loop through the element and inside the if block
db.collectioname.find({findCriteria })
.forEach(function (doc) {
doc.arr.forEach(function (singlearr) {
if (singlearr check) {
singlearr.handled =0
}
});
db.collection.save(doc);
});
I'm amazed this still hasn't been addressed in mongo. Overall mongo doesn't seem to be great when dealing with sub-arrays. You can't count sub-arrays simply for example.
I used Javier's first solution. Read the array into events then loop through and build the set exp:
var set = {}, i, l;
for(i=0,l=events.length;i<l;i++) {
if(events[i].profile == 10) {
set['events.' + i + '.handled'] = 0;
}
}
.update(objId, {$set:set});
This can be abstracted into a function using a callback for the conditional test
The thread is very old, but I came looking for answer here hence providing new solution.
With MongoDB version 3.6+, it is now possible to use the positional operator to update all items in an array. See official documentation here.
Following query would work for the question asked here. I have also verified with Java-MongoDB driver and it works successfully.
.update( // or updateMany directly, removing the flag for 'multi'
{"events.profile":10},
{$set:{"events.$[].handled":0}}, // notice the empty brackets after '$' opearor
false,
true
)
Hope this helps someone like me.
I've been looking for a solution to this using the newest driver for C# 3.6 and here's the fix I eventually settled on. The key here is using "$[]" which according to MongoDB is new as of version 3.6. See https://docs.mongodb.com/manual/reference/operator/update/positional-all/#up.S[] for more information.
Here's the code:
{
var filter = Builders<Scene>.Filter.Where(i => i.ID != null);
var update = Builders<Scene>.Update.Unset("area.$[].discoveredBy");
var result = collection.UpdateMany(filter, update, new UpdateOptions { IsUpsert = true});
}
For more context see my original post here:
Remove array element from ALL documents using MongoDB C# driver
$[] operator selects all nested array ..You can update all array items with '$[]'
.update({"events.profile":10},{$set:{"events.$[].handled":0}},false,true)
Reference
Please be aware that some answers in this thread suggesting use $[] is WRONG.
db.collection.update(
{"events.profile":10},
{$set:{"events.$[].handled":0}},
{multi:true}
)
The above code will update "handled" to 0 for all elements in "events" array, regardless of its "profile" value. The query {"events.profile":10} is only to filter the whole document, not the documents in the array. In this situation it is a must to use $[elem] with arrayFilters to specify the condition of array items so Neil Lunn's answer is correct.
Actually, The save command is only on instance of Document class.
That have a lot of methods and attribute. So you can use lean() function to reduce work load.
Refer here. https://hashnode.com/post/why-are-mongoose-mongodb-odm-lean-queries-faster-than-normal-queries-cillvawhq0062kj53asxoyn7j
Another problem with save function, that will make conflict data in with multi-save at a same time.
Model.Update will make data consistently.
So to update multi items in array of document. Use your familiar programming language and try something like this, I use mongoose in that:
User.findOne({'_id': '4d2d8deff4e6c1d71fc29a07'}).lean().exec()
.then(usr =>{
if(!usr) return
usr.events.forEach( e => {
if(e && e.profile==10 ) e.handled = 0
})
User.findOneAndUpdate(
{'_id': '4d2d8deff4e6c1d71fc29a07'},
{$set: {events: usr.events}},
{new: true}
).lean().exec().then(updatedUsr => console.log(updatedUsr))
})
Update array field in multiple documents in mongo db.
Use $pull or $push with update many query to update array elements in mongoDb.
Notification.updateMany(
{ "_id": { $in: req.body.notificationIds } },
{
$pull: { "receiversId": req.body.userId }
}, function (err) {
if (err) {
res.status(500).json({ "msg": err });
} else {
res.status(200).json({
"msg": "Notification Deleted Successfully."
});
}
});
if you want to update array inside array
await Booking.updateOne(
{
userId: req.currentUser?.id,
cart: {
$elemMatch: {
id: cartId,
date: date,
//timeSlots: {
//$elemMatch: {
//id: timeSlotId,
//},
//},
},
},
},
{
$set: {
version: booking.version + 1,
'cart.$[i].timeSlots.$[j].spots': spots,
},
},
{
arrayFilters: [
{
'i.id': cartId,
},
{
'j.id': timeSlotId,
},
],
new: true,
}
);
I tried the following and its working fine.
.update({'events.profile': 10}, { '$set': {'events.$.handled': 0 }},{ safe: true, multi:true }, callback function);
// callback function in case of nodejs
I am new to Mongodb and spring-data, and I referred to this stackoverflow link grouping quarterly wise and this link using $cond operator in spring data and framed this code snippet below, for retrieving quarterwise sales report in mongodb:
String pipeline = "{$project:{_id:1,'unitsSold':1,'dateSold':1,'results': 1 ,
'productName': 1, 'year':{$year:['$dateSold']}, "+
"'quarter':{$cond:[{$lte:[{$month:'$dateSold'},3]},"+
"'first'," +
"{$cond:[{$lte:[{$month:'$dateSold'},6]},"+
"'second',"+
"{$cond:[{$lte[{$month:'$dateSold'},9]},"+"'third',"+
"'fourth']}]}]}}},"+
"{$group:{'_id':{ 'year':'$year', 'quarter':'$quarter'},
'unitsSold': { $sum: '$unitsSold' },'results':{$push:'$$ROOT'}}}";
DBObject operation = (DBObject)JSON.parse (pipeline);
TypedAggregation<SampleReport> aggregation =newAggregation(SampleReport.class,
new DBObjectAggregationOperation(operation)
);
AggregationResults<SampleReport> result =mongoTemplate.aggregate(aggregation, SampleReport.class);
List<SampleReport> list = result.getMappedResults();
for(SampleReport r : list)
{
System.out.println (r.getProductName() + " : " + r.getUnitsSold() + " : " + r.getQuarter() +":: "+r.getYear());
}
The problem is NOT summing up the units sold. Please lemme know where I am going wrong with spring data. But this query gets the required results using robomongo.
Regards
Kris
If you say it works in another client then likely something got lost in the transation. There are things you can certainly clean up here to make it more simplified.
Can I generally suggest a much more efficient "math" approach to determining the current quarter rather than the current nested conditional statements, as if nothing else it does make things a lot cleaner. In addition of "efficiency" you should not be using $project just preceeding a $group where it makes logical sense to simply combine everything into that one stage:
[
{ "$group": {
"_id": {
"year": { "$year": "$dateSold" },
"quarter": {
"$add": [
{ "$subtract": [
{ "$divide": [{ "$subtract": [{ "$month": "$dateSold" },1]},3]},
{ "$mod": [
{ "$divide": [{ "$subtract": [{ "$month": "$dateSold" },1]},3]},
1
]}
]},
1
]
}
},
"unitsSold": { "$sum": "$unitsSold" }
}}
]
By all means add in your "$push": "$$ROOT" if you really must, but cutting down the involved logic and putting everything into a single pipeline stage where it is logical to do so is largely the point here.
The next phase is that I strongly suggest you code this up natively. Whilst it may be tempting to think that you have a JSON notation that you can use, you will find that in time this is neither flexible nor does it provide very good readability to place in long strings and rely on parsing them. Moreover you are generally going to want to interpolate local variables at some stage
Aggregation aggregation = newAggregation(
new CustomGroupOperation(
new BasicDBObject("$group",
new BasicDBObject("_id",
new BasicDBObject("year",new BasicDBObject("$year","$dateSold"))
.append("quarter",new BasicDBObject(
"$add",Arrays.asList(
new BasicDBObject("$subtract",Arrays.asList(
new BasicDBObject("$divide",Arrays.asList(
new BasicDBObject("$subtract",Arrays.asList(
new BasicDBObject("$month","$dateSold"),
1
)),
3
)),
new BasicDBObject("$mod",Arrays.asList(
new BasicDBObject("$divide", Arrays.asList(
new BasicDBObject("$subtract",Arrays.asList(
new BasicDBObject("$month", "$dateSold"),
1
)),
3
)),
1
))
)),
1
)
))
)
.append("unitsSold", new BasicDBObject("$sum", "$unitsSold"))
)
)
);
You also seem to have abstracted some other code, but I personally prefer to implement the CustomGroupOperation in a way that is not going to conflict with using other spring-mongo aggregation helpers within the newAggregation contruction:
public class CustomGroupOperation implements AggregationOperation {
private DBObject operation;
public CustomGroupOperation (DBObject operation) {
this.operation = operation;
}
#Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
But as originally stated, if you are getting a 0 result then it's field naming or "type" where the field is differently named or in fact a string. But the identical statement should then fail in any other client in a similar way, and the only remedy is to fix the naming or "type" as appropriate.
This is certainly a "cleaner" approach to what you are doing. The "math" is sound for an indexed quarter and can even be adapted to other "financial quarters" as is appropriate via a simple mapping. As does the consolidation of pipeline stages here provide significant performance gains in line with the overall size of the data as a $project means an un-necessary pass through the data just to pre-adjust the fields, which you don't want.
Fix up the definition and execution and then check your fields and data to see that everything is correctly named and typed.