在meteor mongo
shell上我可以使用db.collection.group,但是从Meteor.methods我可以,因为Meteor不支持它.
我怎么能写一个类似于以下方法的方法:
db.orders.group({
keyf: function(doc) {return {year:doc.createdAt.toISOString().substring(0, 4)}},
initial: {months:{}},
reduce: function(order, result) {
var month = order.createdAt.getMonth()+1,
date = order.createdAt.getDate();
month = result.months[month] || (result.months[month] = {});
date = month[date] || (month[date] = []);
date.push(order);
},
cond: {$and: [{createdAt: {$gt: new Date("2015-01-01")}, createdAt: {$lt: new Date("2015-12-31")}}]}
})
Run Code Online (Sandbox Code Playgroud)
期望结果是按月分组的对象组,然后按月内的日期分组.
PS orders
,是一个非常大的集合,我真的想在数据库中进行分组.
在1.0.4中关于rawCollection()的信息后,我尝试了这个:
collection = Orders.rawCollection();
params = {
keyf: function(doc) {return {year:doc.createdAt.toISOString().substring(0, 4)}},
initial: {months:{}},
reduce: function(order, result) {
var month = order.createdAt.getMonth()+1,
date = order.createdAt.getDate();
month = result.months[month] || (result.months[month] = {});
date = month[date] || (month[date] = []);
date.push(order);
},
cond: {$and: [{createdAt: {$gt: new Date("2015-01-01")}, createdAt: {$lt: new Date("2015-12-31")}}]}
};
Meteor.wrapAsync(collection.group, collection)(params);
Run Code Online (Sandbox Code Playgroud)
我越来越:
W20150327-13:26:24.924(2)? (STDERR) /Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/base.js:246
W20150327-13:26:24.924(2)? (STDERR) throw message;
W20150327-13:26:24.924(2)? (STDERR) ^
W20150327-13:26:24.929(2)? (STDERR) TypeError: undefined is not a function
W20150327-13:26:24.929(2)? (STDERR) at /Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/collection/aggregation.js:229:22
W20150327-13:26:24.931(2)? (STDERR) at /Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1191:22
W20150327-13:26:24.931(2)? (STDERR) at /Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1903:9
W20150327-13:26:24.931(2)? (STDERR) at [object Object].Base._callHandler (/Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
W20150327-13:26:24.932(2)? (STDERR) at /Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1758:29
W20150327-13:26:24.932(2)? (STDERR) at [object Object].Connection.write (/Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/connection.js:272:16)
W20150327-13:26:24.932(2)? (STDERR) at __executeQueryCommand (/Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1752:16)
W20150327-13:26:24.932(2)? (STDERR) at Db._executeQueryCommand (/Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1902:7)
W20150327-13:26:24.933(2)? (STDERR) at Db.command (/Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/db.js:1183:8)
W20150327-13:26:24.933(2)? (STDERR) at Collection.group (/Users/jaro/.meteor/packages/mongo/.1.1.0.1jqg8g6++os+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/collection/aggregation.js:228:13)
=> Exited with code: 8
Run Code Online (Sandbox Code Playgroud)
截至Meteor v1.0.4:
通过new
rawCollection
和rawDatabase
methods on提供对npm Mongo驱动程序中的集合和数据库对象的直接访问Mongo.Collection
因此,您可以调用collection.rawCollection()
以获取基础集合对象:
var rawCollection = Orders.rawCollection();
Run Code Online (Sandbox Code Playgroud)
这rawCollection
有一个方法group
,它等同group
于MongoDB shell中的方法.但是,底层节点API是异步的,因此您需要以某种方式将其转换为同步函数.我们不能Meteor.wrapAsync
直接使用因为group
获取不是主要回调的函数参数,所以我们将使用包装器来解决这个问题:
function ordersGroup(/* arguments */) {
var args = _.toArray(arguments);
return Meteor.wrapAsync(function (callback) {
rawCollection.group.apply(rawCollection, args.concat([callback]));
})();
}
Run Code Online (Sandbox Code Playgroud)
在你的方法中,你可以ordersGroup
像db.orders.group
在Mongo shell中一样调用.但是,参数是分开传递的,而不是在对象中传递:
ordersGroup(keys, condition, initial, reduce[, finalize[, command[, options]]])
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅此文档(尽管请注意,callback
应该省略该参数,因为我们的异步包装会处理这个问题).
所以你必须单独传递它们:
var result = ordersGroup(
// keys
function(doc) {
return { year: doc.createdAt.toISOString().substring(0, 4) };
},
// condition
{createdAt: {$lt: new Date("2015-12-31"), $gt: new Date("2015-01-01")}},
// initial
{months: {}},
// reduce
function(order, result) {
var month = order.createdAt.getMonth()+1,
date = order.createdAt.getDate();
month = result.months[month] || (result.months[month] = {});
date = month[date] || (month[date] = []);
date.push(order);
}
);
Run Code Online (Sandbox Code Playgroud)
当然,这仅适用于服务器,因此请确保您的方法仅在服务器代码中(最好在server
子目录中,或在内部if (Meteor.isServer)
).
归档时间: |
|
查看次数: |
1241 次 |
最近记录: |