Javascript 如何将mongo查询转换为java代码
我有一些复杂的javascript代码来查询MongoShell中的数据。我怎么能不费吹灰之力就得到这个java等价物呢?我已经阅读了很多教程,但似乎都需要付出巨大的努力才能获得与查询相同的结果Javascript 如何将mongo查询转换为java代码,javascript,java,mongodb,mongo-java,Javascript,Java,Mongodb,Mongo Java,我有一些复杂的javascript代码来查询MongoShell中的数据。我怎么能不费吹灰之力就得到这个java等价物呢?我已经阅读了很多教程,但似乎都需要付出巨大的努力才能获得与查询相同的结果 var trendForDate = new Date('2016-01-08T18:30:00.000Z'); var oneMonthBack = new Date(trendForDate.getTime() - 1000 * 60 * 60 * 24 * 31); var trendToDa
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" : 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" }
.....
您是否可以编辑您的问题以显示一些示例文档以及这些示例文档的预期聚合输出是什么?不考虑最终语言实现,如果您打算通过该函数调用在MongoDB服务器上运行
mapReduce
作业,则“mapper”和“reducer”实现仍将处于运行状态“JavaScript”。因此,如果我编写Perl/Python/C++甚至Java,那么函数仍然是用JavaScript编写的(通常作为“字符串”)并发送到服务器执行。因此没有任何变化。除了上面的代码显然已被破坏之外(您无法从“reducer”输出与“mapper”不同的结构),但这实际上是一个不同的问题。@chridam,编辑了这个问题以显示输出,即您想要的预期输出还是实际输出?一些要测试的示例文档如何?这是我得到的实际输出,我想要完全相同的输出
{ "_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" }
.....