我有一个复杂的javascript代码来查询来自mongo shell的数据。如何在没有太多努力的情况下获得这方面的java等价,我经历了很多教程,但似乎都需要付出巨大的努力才能从查询中获得相同的结果
var trendForDate = new Date('2016-01-08T18:30:00.000Z');
var oneMonthBack = new Date(trendForDate.getTime() - 1000 * 60 * 60 * 24 * 31);
var trendToDate = new Date(trendForDate.getTime() + 1000 * 60 * 60 * 24);
var loyal = "loyal";
var newCustomer = "newCustomer";
var loyalLost = "loyalLost";
var dormant = "dormant";
var lost = "lost";
var usersForOrderDate = db.order.find({
"status": "DELIVERED",
"orderDate.interval_start": {
$gte: trendForDate,
$lt: trendToDate
}
}).map(function(o) {
return o.customer
});
db.order.mapReduce(function() {
emit(this.customer, {
count: 1,
orderDate: this.orderDate.interval_start
})
}, function(key, values) {
var sum = 0;
var lastOrderDate;
var category;
values.forEach(function(value) {
if (value['orderDate']) {
lastOrderDate = value['orderDate'];
} else {
lastOrderDate = value['lastOrderDate'];
}
sum += value['count'];
});
if (sum >= 5 & lastOrderDate > oneMonthBack) {
category = loyal;
}
if (sum <= 4 & sum >= 1 & lastOrderDate > oneMonthBack) {
category = newCustomer;
}
if (sum >= 5 & lastOrderDate < oneMonthBack) {
category = loyalLost;
}
if (sum <= 4 & sum > 1 & lastOrderDate < oneMonthBack) {
category = dormant;
}
if (sum == 1 & lastOrderDate < oneMonthBack) {
category = lost;
}
return {
count: sum,
lastOrderDate: lastOrderDate,
category: category
};
}, {
query: {
status: "DELIVERED",
"orderDate.interval_start": {
$lt: trendForDate
}
},
out: {
merge: "customerTrend"
},
finalize: function(key, value) {
if (value.count == 1) {
value.category = lost;
}
return value;
},
scope: {
oneMonthBack: oneMonthBack,
trendDate: trendForDate,
loyal: loyal,
newCustomer: newCustomer,
loyalLost: loyalLost,
dormant: dormant,
lost: lost
}
});
print(trendForDate);
print(trendToDate);
db.customerTrend.aggregate([{
$match: {
_id: {
$in: usersForOrderDate
}
}
}, {
$group: {
"_id": "$value.category",
"users": {
$sum: 1
}
}
}]);
编辑:
通过此查询,我得到如下输出
{ "_id" : "dormant", "users" : 7 }
{ "_id" : "lost", "users" : 9 }
{ "_id" : "newCustomer", "users" : 53 }
{ "_id" : "loyalLost", "users" : 23 }
{ "_id" : "loyal", "users" : 160 }
要为查询运行的示例文档
{ "_id" : ObjectId("546b1018e4b0ce7d5ee021ce"), "customer" : ObjectId("546b0fdde4b0ce7d5ee021cc"), "orderDate" : { "jodaType" : "org.joda.time.DateTime", "interval_start" : ISODate("2014-11-17T18:30:00Z"), "interval_end" : ISODate("2014-11-17T18:30:00Z"), "jodaField_zone" : "UTC", "time" : NumberLong("1416249000000"), "jodaField_dayOfMonth" : 17, "jodaField_hourOfDay" : 18, "jodaField_minuteOfHour" : 30, "jodaField_millisOfSecond" : 0, "jodaField_monthOfYear" : 11, "jodaField_year" : 2014 }, "status" : "DELIVERED" }
{ "_id" : ObjectId("546b6d87e4b0ce7d5ee021e0"), "customer" : ObjectId("545f64e7e4b07a0a501276db"), "orderDate" : { "jodaType" : "org.joda.time.DateTime", "interval_start" : ISODate("2014-11-17T18:30:00Z"), "interval_end" : ISODate("2014-11-17T18:30:00Z"), "jodaField_zone" : "UTC", "time" : NumberLong("1416249000000"), "jodaField_dayOfMonth" : 17, "jodaField_hourOfDay" : 18, "jodaField_minuteOfHour" : 30, "jodaField_millisOfSecond" : 0, "jodaField_monthOfYear" : 11, "jodaField_year" : 2014 }, "status" : "DELIVERED" }
{ "_id" : ObjectId("546c13e3e4b0ce7d5ee021f0"), "customer" : ObjectId("54696c64e4b07f3c07010b4a"), "orderDate" : { "jodaType" : "org.joda.time.DateTime", "interval_start" : ISODate("2014-11-18T18:30:00Z"), "interval_end" : ISODate("2014-11-18T18:30:00Z"), "jodaField_zone" : "UTC", "time" : NumberLong("1416335400000"), "jodaField_dayOfMonth" : 18, "jodaField_hourOfDay" : 18, "jodaField_minuteOfHour" : 30, "jodaField_millisOfSecond" : 0, "jodaField_monthOfYear" : 11, "jodaField_year" : 2014 }, "status" : "DELIVERED" }
{ "_id" : ObjectId("546c2357e4b0ce7d5ee021fb"), "customer" : ObjectId("546c2208e4b0ce7d5ee021f9"), "orderDate" : { "jodaType" : "org.joda.time.DateTime", "interval_start" : ISODate("2014-11-18T18:30:00Z"), "interval_end" : ISODate("2014-11-18T18:30:00Z"), "jodaField_zone" : "UTC", "time" : NumberLong("1416335400000"), "jodaField_dayOfMonth" : 18, "jodaField_hourOfDay" : 18, "jodaField_minuteOfHour" : 30, "jodaField_millisOfSecond" : 0, "jodaField_monthOfYear" : 11, "jodaField_year" : 2014 }, "status" : "DELIVERED" }
{ "_id" : ObjectId("54b0db4ee4b08e29cd9309e0"), "customer" : ObjectId("549e790fe4b09508addfcc34"), "orderDate" : { "jodaType" : "org.joda.time.DateTime", "interval_start" : ISODate("2015-01-09T18:30:00Z"), "interval_end" : ISODate("2015-01-09T18:30:00Z"), "jodaField_zone" : "UTC", "time" : NumberLong("1420828200000"), "jodaField_dayOfMonth" : 9, "jodaField_hourOfDay" : 18, "jodaField_minuteOfHour" : 30, "jodaField_millisOfSecond" : 0, "jodaField_monthOfYear" : 1, "jodaField_year" : 2015 }, "status" : "DELIVERED" }
.....