2017-10-11 103 views
0

我正在构建一个活动应用程序,并希望在某个日期获取所有活动,然后按照那里对我进行排序。如何实现这样的请求?这是架构的样子:MongoDB按日期查询并按距离排序(Mongoose + Express App)

var eventSchema = mongoose.Schema({ 
_id: { 
    type: String, 
    required: true 
}, 
name: { 
    type: String, 
    required: true 
}, 
start_time: { 
    type: Date 
}, 
location: { 
    type: { 
     type: String 
    }, 
    coordinates: [] 
}, 
}); 

我知道我可以使用$ near查询位置,我设法让它运行。但我想首先查询“start_time”,然后根据它们与我的距离排序该日期的事件。任何建议将非常感激,谢谢;)

回答

0

我设法得到它与此查询工作:

{ 
    location: { 
     $near : { 
     $geometry: { type: "Point", coordinates: [longitude,latitude] 
     }, 
     $maxDistance: 10000 
     } 
    }, 
    $and: [ 
     { start_time : {$gte: now, $lt: tomorrow} } 
    ] 
} 
1

使用$geoNear agg管道运营商。它将按照您指定的点的顺序返回文档。您可以通过调用$geoNear进一步将查找限制为start_time(或任意查询表达式),并在调用中使用query选项。详情点击这里: https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/

+0

感谢,这是有点复杂,我的小例子,我得到了它与$和运营商和扭曲的工作“排序”一下。不管怎么说,多谢拉 –