如何从mapReduce mongoose中的关键字获得近似值?

我在MongoDB中有一些文件,例如:

{ "_id" : ObjectId("587f41ffb5ffa43ec25aa075"), "IP" : "113.183.149.127", "Product_Version" : "CMC_2.0", "Product_ID" : "HHFFHJRLAUHOZDBM1YWY291NM0DD7J40", "OS" : "Microsoft_Window_7", "virus" : "Adware:W32/Superfish", "Time" : ISODate("2017-01-18T10:22:55.356Z"), "city_name" : "", "location" : { "latitude" : 0.0, "longitude" : 0.0 } } { "_id" : ObjectId("587f41ffb5ffa43ec25aa078"), "IP" : "27.76.109.254", "Product_Version" : "CMC_2.0", "Product_ID" : "UCOV8JY5YHKQ2B0HPCC0IM1S9T0ICWL7", "OS" : "Microsoft_Window_7", "virus" : "Backdoor:W32/BlackEnergy", "Time" : ISODate("2017-01-18T10:22:55.380Z"), "city_name" : "Ho Chi Minh City", "location" : { "latitude" : 10.75, "longitude" : 106.66667 } } { "_id" : ObjectId("587f41ffb5ffa43ec25aa079"), "IP" : "42.116.189.121", "Product_Version" : "CMC_2.0", "Product_ID" : "3WKR7313ONV21OXNX1ZGDKG0A0CB89MH", "OS" : "Microsoft_Window_7", "virus" : "Trojan:W97M/MaliciousMacro", "Time" : ISODate("2017-01-18T10:22:55.440Z"), "city_name" : "Hanoi", "location" : { "latitude" : 21.0245, "longitude" : 105.84117 } } 

我使用mapReduce查找date("2017-01-18")所有文档

 var query = {}; query.map = function() { var key = new Date("2017-01-18"); var value = { lat: this.location.latitude, lng: this.location.longitude, }; emit(key, value); }; query.reduce = function(key, values) { var reducedObject = {}; values.forEach(function(value) { reducedObject.lat = value.lat; reducedObject.lng = value.lng; }); return reducedObject; }; query.out = { replace: "map_reduce" }; query.verbose = true; Msg.mapReduce(query, function(error, model, stats) { if (error) { require(path.join(__dirname, '../', 'ultis/logger.js'))().log('error', JSON.stringify(error)); if (typeof callback === 'function') return callback(-2, null); } else { console.log('map reduce took %d ms', stats.processtime) model.find().exec(function(error, docs) { if (error) { require(path.join(__dirname, '../', 'ultis/logger.js'))().log('error', JSON.stringify(error)); if (typeof callback === 'function') return callback(-2, null); } else { // if (typeof callback === 'function') return callback(null, docs); console.log(docs); } }); } }); 

我只是有这样的结果

 map reduce took 855 ms [ { _id: Wed Jan 18 2017 07:00:00 GMT+0700 (SE Asia Standard Time), value: { lat: 0, lng: 0 } } ] 

我想在2017-01-18有所有文件。

问题1:2017-01-18如何获得所有文件?

问题2:mapReduce可以通过key获取值是一个对象吗? 我的意思是我可以从2017-01-18到2017-01-23之间获得文件。 像这样的图像

 query.map = function() { var key = {}; key.from = new Date("2017-01-18"); key.to= new Date("2017-01-23"); var value = { lat: this.location.latitude, lng: this.location.longitude, }; emit(key, value); }; 

MapReduce是这个任务矫枉过正。 您只需构build一个从2017-01-18 00:00hrs2017-01-18 23:59:59.999开始的date范围查询,然后在find()aggregate()pipe道查询中使用它:

 var start = new Date("2017-01-18"); start.setHours(0,0,0,0); var end = new Date("2017-01-18"); end.setHours(23,59,59,999); var query = { "Time": { "$gte": start, "$lte": end } }; Msg.find(query, function(error, docs) { if (error) { console.log(error); } else { console.log(docs); } }); 

或使用aggregate()

 Msg.aggregate() .match(query) .exec(function(error, docs) { if (error) { console.log(error); } else { console.log(docs); } }); 

对于第二个问题,只是将查询date范围更改为

 var start = new Date("2017-01-18"); var end = new Date("2017-01-23"); var query = { "Time": { "$gte": start, "$lte": end } };