mongodb - How to run a .group() with Java -
i have query in mongodb , produces result want. i'm trying use in java.
this query in mongodb:
var red = function(doc, out) { out.count_order++; out.sum_qty += doc.quantity; out.sum_base_price += doc.extendedprice; out.sum_disc_price += doc.extendedprice * (1 - doc.discount); out.sum_charge += doc.extendedprice * (1 - doc.discount) * (1 + doc.tax); out.avg_disc += doc.discount; }; var avg = function(out) { out.avg_qty = out.sum_qty / out.count_order; out.avg_price = out.sum_base_price / out.count_order; out.avg_disc = out.avg_disc / out.count_order; }; db.lineitems.group( { key : { returnflag : true, linestatus : true}, cond : { "shipdate" : {$lte: 19980801}}, initial: { count_order : 0, sum_qty : 0, sum_base_price : 0, sum_disc_price : 0, sum_charge : 0, avg_disc : 0}, reduce : red, finalize : avg });
and way i'm using in java don't know how use avg function.
string avg = "var avg = function(out) {" + "out.avg_qty = out.sum_qty / out.count_order;" + "out.avg_price = out.sum_base_price / out.count_order;" + "out.avg_disc = out.avg_disc / out.count_order;};"; string reduce = "function(doc, out) {" + "out.count_order++;" + "out.sum_qty += doc.quantity;" + "out.sum_base_price += doc.extendedprice;" + "out.sum_disc_price += doc.extendedprice * (1 - doc.discount);" + "out.sum_charge += doc.extendedprice * (1 - doc.discount) * (1 + doc.tax);" + "out.avg_disc += doc.discount;};"; string finalize = "function(out) {" + "out.avg_qty = out.sum_qty / out.count_order;" + "out.avg_price = out.sum_base_price / out.count_order;" + "out.avg_disc = out.avg_disc / out.count_order;};"; mapreduceiterable<document> iterable = collection.mapreduce(**????**, reduce).finalizefunction(finalize);
how can use function?
you better off using aggreation framework calling .group()
method, of course older implementation before aggregation framework existed.
the logic applied in shell be:
db.lineitems.aggregate([ { "$match": { "shipdate": { "$lte": 19980801 } }, { "$group": { "_id": { "returnflag": "$returnflag", "linestatus": "$linestatus" }, "count": { "$sum": 1 }, "sum_qty": { "$sum": "$quantity" }, "avg_qty": { "$avg": "$quantity" }, "sum_base_price": { "$sum": "$extendedprice" }, "avg_base_price": { "$avg": "$extendedprice" }, "sum_disc_price": { "$sum": { "$multiply": [ "$extendedprice", { "$subtract": [ 1, "$discount" ] } ] } }, "avg_disc_price": { "$avg": { "$multiply": [ "$extendedprice", { "$subtract": [ 1, "$discount" ] } ] } }, "sum_charge": { "$sum": { "$multiply": [ "$extendedprice", { "$subtract": [ 1, "$discount" ] }, { "$add": [ 1, "$tax" ] } ] } }, "avg_disc": { "$avg": "$discount" } }} ])
the aggregation framework efficient $group
pipeline stage operator same things in native coded operations. there $sum
, $avg
accumulators built in other math operations.
in short, "natively coded" , not relying on "javascript interpretation", execution faster .group()
can provide. plus should straightforward understand.
translating java like
list<document> pipeline = arrays.<document>aslist( new document( "$match", new document( "shipdate", new document( "$lte", 19980801 ) ) ), new document( "$group", new document( "_id", new document( "returnflag", "$returnflag" ).append( "linestatus", "$linestatus" ) ).append( "count", new document( "$sum", 1 ) ).append( "sum_qty", new document( "$sum", "$quantity" ) ).append( "avg_qty", new document( "$avg", "$quantity" ) ).append( "sum_base_price", new document( "$sum", "$extendedprice" ) ).append( "avg_base_price", new document( "$avg", "$extendedprice" ) ).append( "sum_disc_price", new document( "$sum", new document( "$multiply", arrays.aslist( "$extendedprice", new document( "$subtract", arrays.aslist(1, "$discount") ) ) ) ) ).append( "avg_disc_price", new document( "$avg", new document( "$multiply", arrays.aslist( "$extendedprice", new document( "$subtract", arrays.aslist(1, "$discount") ) ) ) ) ).append( "sum_charge", new document( "$sum", new document( "$multiply", arrays.aslist( "$extendedprice", new document( "$subtract", arrays.aslist(1, "$discount") ), new document( "$add", arrays.aslist(1, "$tax") ) ) ) ) ).append( "avg_disc", new document( "$avg", "$discount" ) ) ) ); aggregateiterable<document> result = collection.aggregate(pipeline);
Comments
Post a Comment