下面是我的发布函数,它几乎是Average Aggregation Queries in Meteor的副本
用途:显示文档列表。然后,用户可以单击一个项目(餐厅),并可以查看详细信息。在详细信息中,有上一个和下一个箭头,这将允许循环通过列表。
我的想法是使用聚合方法来用上一个和下一个文档id来丰富每个文档,这样我就可以轻松地构建我的上一个和下一个链接。
但是怎么做呢?我没有头绪。也许有更好的方法?
Meteor.publish("restaurants", function(coords, options) {
if (coords == undefined) {
return false;
}
if (options == undefined) {
options = {};
}
if (options.num == undefined) {
options.num = 4
}
console.log(options)
/**
* the following code is from https://stackoverflow.com/questions/18520567/average-aggregation-queries-in-meteor
* Awesome piece!!!
*
* */
var self = this;
// This works for Meteor 1.0
var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
// Your arguments to Mongo's aggregation. Make these however you want.
var pipeline = [
{
$geoNear: {
near: { type: "Point", coordinates: [ coords.lng , coords.lat ] },
distanceField: "dist.calculated",
includeLocs: "dist.location",
num: options.num,
spherical: true
}
}
];
db.collection("restaurants").aggregate(
pipeline,
// Need to wrap the callback so it gets called in a Fiber.
Meteor.bindEnvironment(
function(err, result) {
// Add each of the results to the subscription.
_.each(result, function(e) {
e.dist.calculated = Math.round(e.dist.calculated/3959/0.62137*10)/10;
self.added("restaurants", e._id, e);
});
self.ready();
},
function(error) {
Meteor._debug( "Error doing aggregation: " + error);
}
)
);
});发布于 2014-12-04 10:59:08
如果你没有太多关于结果的文档,你可以用JavaScript来实现。将您的订阅代码更改为类似这样的代码(我没有测试此代码)。
_.each(result, function(e, idx) {
if(result[idx - 1]) e.prev = result[idx - 1]._id;
if(result[idx + 1]) e.next = result[idx + 1]._id;
e.dist.calculated = Math.round(e.dist.calculated/3959/0.62137*10)/10;
self.added("restaurants", e._id, e);
});https://stackoverflow.com/questions/26861070
复制相似问题