如何在文档mongodb中查找和排序文档

时间:2018-10-17 06:28:32

标签: javascript html node.js mongodb

我有带嵌套文档的mongodb数据库。我需要在嵌套文档日期字段中获得最整洁的时间值。

 {
    "_id": "5bc4784897d4321c34eaa114",
    "vehicleNumber": "wp PO 2512",
    "imeiNumber": "356307045861738",
    "userId": "5bc4778445ad802a783c89be",
    "trackingData": [
        {
            "_id": "5bc6aa41b76d90328c3b9ef7",
            "date": "Wed Oct 17 2018 08:51:13 GMT+0530 (Sri Lanka Standard Time)",
            "longitude": "79.8811638",
            "latitude": "6.8759496",
            "altitude": "187",
            "angle": "15",
            "satelites": "9",
            "speed": "0"
        },
        {
            "_id": "5bc6ab27276438026c03ee88",
            "date": "Wed Oct 17 2018 08:54:33 GMT+0530 (Sri Lanka Standard Time)",
            "longitude": "79.8811638",
            "latitude": "6.8759496",
            "altitude": "70",
            "angle": "15",
            "satelites": "9",
            "speed": "0"
        },
        {
            "_id": "5bc6abcc276438026c03ee89",
            "date": "Wed Oct 17 2018 08:57:53 GMT+0530 (Sri Lanka Standard Time)",
            "longitude": "79.8811638",
            "latitude": "6.8759496",
            "altitude": "69",
            "angle": "15",
            "satelites": "9",
            "speed": "0"
        },
        {
            "_id": "5bc6ac9b276438026c03ee8a",
            "date": "Wed Oct 17 2018 09:01:13 GMT+0530 (Sri Lanka Standard Time)",
            "longitude": "79.8811638",
            "latitude": "6.8759496",
            "altitude": "63",
            "angle": "15",
            "satelites": "7",
            "speed": "0"
        }

}

这是我的mongodb收藏。我的模特想喜欢

//sort tracking data by date
module.exports.sortTrackingData = function(imeiNumber, callback){
    console.log(imeiNumber);
    Vehicle.find({'imeiNumber':imeiNumber}).sort({'trackingData.date': 1}).limit(1);
}

和我的控制器

sortByDate: function(req,res){
        var imeiNumber = req.params.imeiNumber;
        Vehicle.sortTrackingData(imeiNumber,function(err, vehicleRes){
            if (err){
                throw err;
            }
            else{
                res.json(vehicleRes);
            }
        });
    }

我需要使用跟踪数据内部的日期字段过滤出一个值。只有我们需要在文档中引用经度和纬度值。

0 个答案:

没有答案