我试图在聚合查询中使用 $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)]: {} }