$ geoNear聚合中无法使用线串

时间:2019-05-05 06:24:13

标签: node.js mongodb mongoose mongodb-geospatial

我试图在聚合查询的$ geoNear中使用LineString,但无法使用它。我希望其他任何方法都可以使用LineString来比较坐标列表。

这是我的功能

function SentNotifications(coordinates,userid,startdate,enddate)
{
    var listofusers=[];
    console.log(coordinates);
    TripCoordinatesData.aggregate([
        {
            $geoNear: {
              near: { type: "Point", coordinates: coordinates},
              distanceField: "calculated",
              maxDistance: 2
           }
       },       
       { 
           $unwind: "$calculated" 
       },
        {
          $lookup:
            {
              from: "tripdatas",
              localField: "tripdata",
              foreignField: "_id",
              as: "trip"
            }
       },
       { $unwind: "$trip" },

       {
          $lookup:
            {
              from: "userdatas",
              localField: "trip.userdata",
              foreignField: "_id",
              as: "user"
            }
       },
         { $unwind: "$user" },

       {
            $match:
            {
                "trip.userdata":{$ne:mongoose.Types.ObjectId(userid)},
                "trip.createdon":{"$lte" : new Date(startdate)},
                "trip.createdon":{"$gte" : new Date(enddate)},

                //Logged in userid
                "trip.NotificationsSentTo": {$nin:[mongoose.Types.ObjectId(userid)]} 
            }
        },
//        {$group:{_id:{user:{firebaseToken:"$user.firebaseToken"}, trip:'$trip',calculated:'$calculated'}}}
        {$group:{ _id: null,
      users: {
        $addToSet: "$user.firebaseToken"
      }}}

     ],function(err,result){       
         console.log(result);
         console.log(err);

     });
}

这是我的要求:

{
"id":"0",
"data":[ 
            [
            73.67368340492247,
            18.728638338108293
          ],
          [
            73.67421984672546,
            18.729959229560155
          ]
        ]
}

这是我在函数中传递数据的方式:

SentNotifications(req.body.data,tokenhelper.getUserFromToken(req.headers.token),Date.UTC("2019-05-04T00:00:00Z"),Date.UTC("2019-05-04T23:59:59Z"));

这是我的mongo文件:

/* 1 */
{
    "_id" : ObjectId("5ccda8cd297d56360402ff82"),
    "location" : {
        "coordinates" : [ 
            [ 
                73.6736834049225, 
                18.7286383381083
            ], 
            [ 
                73.6742198467255, 
                18.7299592295602
            ]
        ],
        "type" : "LineString"
    },
    "tripdata" : ObjectId("5ccda8cd297d56360402ff81"),
    "createdon" : ISODate("2019-05-04T14:59:25.720Z"),
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5ccda96344bdeb24bcdf66f6"),
    "location" : {
        "coordinates" : [ 
            [ 
                73.6736834049225, 
                18.7286383381083
            ], 
            [ 
                73.6742198467255, 
                18.7299592295602
            ]
        ],
        "type" : "LineString"
    },
    "tripdata" : ObjectId("5ccda96344bdeb24bcdf66f5"),
    "createdon" : ISODate("2019-05-04T15:01:55.942Z"),
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("5ccda98044bdeb24bcdf66f7"),
    "location" : {
        "coordinates" : [ 
            [ 
                73.6736834049225, 
                18.7286383381083
            ], 
            [ 
                73.6742198467255, 
                18.7299592295602
            ]
        ],
        "type" : "LineString"
    },
    "tripdata" : ObjectId("5ccda96344bdeb24bcdf66f5"),
    "createdon" : ISODate("2019-05-04T15:02:24.482Z"),
    "__v" : 0
}

所以我需要一个可以在聚合函数中使用线串的解决方案。 因为我避免将for循环作为一种不好的做法。

编辑: 我想比较两个线串,一个来自集合,一个来自请求,如果线串之间的距离小于或等于1000米,则该集合应该返回,这是我的基本要求。

编辑2: 我尝试过:

     $geoNear:                                                                        
            {
              near: { type: "LineString", coordinates: coordinates},
              distanceField: "calculated",
              maxDistance: 2
           }  

但是出现此错误:

{ MongoError: 'near' field must be point
    at Connection.<anonymous> (D:\Projects\TrackingApp\trackingapp\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:443:61)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at processMessage (D:\Projects\TrackingApp\trackingapp\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:364:10)
    at Socket.<anonymous> (D:\Projects\TrackingApp\trackingapp\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:533:15)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:594:20)
  ok: 0,
  errmsg: '\'near\' field must be point',
  code: 17304,
  codeName: 'Location17304',
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }   

0 个答案:

没有答案
相关问题