在计算单位米之间距离的场景中,协作者通常指的是参与计算的两个或多个对象。存储和查询这些协作者的信息需要考虑以下几个方面:
解决方法:
CREATE TABLE collaborators (
id INT PRIMARY KEY,
name VARCHAR(255),
location POINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
{
"_id": "unique_id",
"name": "Collaborator Name",
"location": {
"type": "Point",
"coordinates": [longitude, latitude]
},
"created_at": "timestamp"
}
解决方法:
SELECT
name1,
name2,
ST_Distance_Sphere(location1, location2) AS distance
FROM
(SELECT
c1.name AS name1,
c2.name AS name2,
c1.location AS location1,
c2.location AS location2
FROM
collaborators c1,
collaborators c2
WHERE
c1.id <> c2.id AND
ST_Distance_Sphere(c1.location, c2.location) < 10000) AS subquery;
db.collaborators.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [longitude, latitude] },
distanceField: "distance",
spherical: true,
maxDistance: 10000
}
}
]);
通过上述方法,可以有效地存储和查询用于计算单位米之间距离的协作者信息。
领取专属 10元无门槛券
手把手带您无忧上云