请考虑以下几点:
db.stores.aggregate(
{ $project: {
_id: 0,
in_radius: {
$cond: {
if: <geoWithinExpression>,
then: 1,
else: 0
}
}
}
})
geoWithinExpression实际上是:
$match: {
location: {
$geoWithin: {
$center: [[lat, lon], radius]
}
}
}
我是这样做的,所以我可以在结果集中使用$group来计算半径内的商店数量。(我使用的是$project,因为我想做更多像这样的自定义列,并在一次中全部返回)。
这有可能吗?
发布于 2014-06-04 04:28:56
最后,通过使用聚合接口,使用$geoNear
在$group
阶段的$cond
中添加的$cond
,我能够准确地返回所需的内容:
[
{
$geoNear: {
near: [<lon>,<lat>],
distanceField: "distance",
spherical: true,
maxDistance: <extended_radius_distance_in_radians>
distanceMultiplier: earthRadius
}
},
{
$group:{
_id: "default",
in_default_radius: { $sum: { $cond: { if: { $lt: ["$distance", <default_radius>] }, then: 1, else: 0 } } },
in_extended_radius: { $sum: 1 },
in_extended_radius_with_a_cond: { $sum: { $cond: { if: { $eq: ["$something", "something"] }, then: 1, else: 0 } } }
}
}
]
发布于 2014-05-29 15:40:45
这是不可能的,因为许多理由都是相当合理的,真的。
如前所述,这个概念是相当合理的,因为在其他时间,除了最初的管道阶段,您实际上将拥有与创建索引的文档相匹配的任何内容。新文档“副本”仅存在于管道处理中,因此没有关联的索引。
虽然很明显,您的意图是“测试”不同集合中位置的数据,但最好的方法是使用几个查询进行测试,并将结果合并到代码中,或者以其他方式写入另一个集合。
https://stackoverflow.com/questions/23941849
复制相似问题