获取NodeJS中两个位置之间的记录

时间:2017-11-03 07:09:07

标签: node.js mongodb reactjs mongoose

如何使用mongoose在NodeJS中的两个位置之间获取记录。我正在使用函数来查找功能如

的记录
 getAllEvent: function (locationReq, callback) {    
        Events.find({
        location: {
            $near: {
                $geometry: {
                    loc: locationReq
                },
                $maxDistance: 0,
                $minDistance: 250
            }
        }
    }, {isDeleted: false}, function (error, data) {
        callback((error) ? {} : (data == null) ? {} : data);
    });
    },

此函数返回null值。

特定记录的位置也存储在数据库文档的集合中。在LocationReq中将发送两个位置值。如果记录在这两个位置之间,我想要记录。

数据库文档如下,

{
    "_id" : ObjectId("59db3a1e56e60915086fb73f"),
    "eventName" : "5643213246",
    "createdId" : "599d698ffa23343efe9c5cbb",
    "eventDateTime" : ISODate("2017-08-09T00:00:00.000Z"),
    "startTime" : ISODate("2017-08-16T13:01:52.379Z"),
    "endTime" : ISODate("2017-08-16T13:01:52.379Z"),
    "location" : {
        "lat" : "59.9500",
        "lng" : "30.3300"
    },
    "description" : "description123",
    "category" : "59db12dab928f105a87348f6",
    "host" : "host",
    "phoneNumber" : 123123,
    "isDisabled" : false,
    "isDeleted" : true,
    "dateCreated" : ISODate("2017-10-09T08:58:06.687Z"),
    "__v" : 0,
    "updatedAt" : ISODate("2017-10-24T06:47:07.966Z")
}

1 个答案:

答案 0 :(得分:0)

getAllEvent: function (locationReq, callback) {    
        Events.find({
        loc: {
             $nearSphere: {
                   $geometry: {type: "Point", coordinates: [lng, lat]},
                   $maxDistance:distance
                          }

    }, {isDeleted: false}, function (error, data) {
        callback((error) ? {} : (data == null) ? {} : data);
    });
    },

将位置参数替换为loc