2017-10-11 105 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

感謝,這是有點複雜,我的小例子,我得到了它與$和運營商和扭曲的工作「排序」一下。不管怎麼說,多謝拉 –