From the MongoDB documentation for this example, how can I write queries using MongoTemplate?
{$ $ group: {_id: {month: {$ month: "$ date"}, day: {$ dayOfMonth: "$ date"}, year: {$ Year: $ Date:}}, total value: {$ sum: {$ multiply: ["$ price", "$ quantity"]}}, average amount: $ $ average: "$ quantity"}, calculation: {$ sum: 1}}}])
Or in general, how do I group from a calculated field?
You can actually do something like "project", but for me it's a little Intuitive that requires $ project
step <("days"). ("Year") and expression ("year (date)") ("year") and expression ("day"). And expression (" ("Total amount"). ("Month")); as I said, As per intuitive as you should be able to declare all this under the $ group
phase, but the helpers do not work like this. Some of the serialization It comes out of the fun (the date operator wraps the archers with arrays), but it seems to work. But still, it is rather two pipeline steps instead of one.
What is the problem with separation of steps? By separating the steps, the steps of the "Project" part are forced to process all the documents in the pipeline so that the calculated fields can be obtained, this means that the group level Everything goes through before going on.
The difference in processing time can be seen clearly by running questions in both forms. With a different project phase, query on my hardware Comparison can take three times more time to execute, where all the fields are calculated during the "group" operation.
It seems that there is only one way to properly create the pipeline object yourself:
ApplicationContext ctx = New Entatation Config Applications Contex (SpringMongo Config. Class); MongoOperations Mongolia Operation = (MongoOperations) ctx.getBean ("MongoTemplate"); BasicDBList Pipeline = New BasicDBList (); String [] multiplier = {"$ value", "$ quantity"}; Pipeline.ed (new "Basic" ("$ month", "$ date"), new basebobjects ("$ group", new BasicBobbasex ("_ id", new basic bidbox ("month", new basicblog ("$ month", "$ date"). ("$ Day", "$ date") .append ("year", new BasicDBObject ("$ year", "$ date")) .append ("Total value", New BasicDBObject ("$ sum", New BasicDBObject ("$ Multiply", multiplier)) .append ("Avg Option", New BasicDBObject ("$ avg", "$ quantity")). Append ("count", New BasicDBObject ("$ sum", 1) )); BasicDBObject aggregation = new BasicDBObject ("total", "archive") .append ("pipeline", pipeline); println (aggregation ;); CommandResult Command Result = Mongo Operation. Execute Commands;
Or if you seem to summarize all, you can always work with JSON source and pars But, of course, it should be a valid JSON:
string jason = "[" + "{\" $ group \ ": {" + "\" _ id \ "": {"" \ "Day \": {\ "$ month \": \ "$ date \"}, "+" \ "day \": {\ "$ dayOfMonth \": \ "$ date \ "Year \": {\ "$ year \": \ "$ date \"} "+"}, "+" \ "total value \": {\ "$ sum \" : {\ "$ Multiply \" "[\" $ Price \ ", \" $ quantity \ "]}," + "\" av "\": \ "Count \": {\ "$ sum \": 1} "+"}} "+"] "erageQuantity \": {\ "$ avg \": \ "$ quantity \"}, "+" \ "count \": ; Basic DB List Pipeline = (Basic Debianist) MangodeBtil.JSON.Parse (JSN);
No comments:
Post a Comment