我正在使用节点(使用 MongoClient 库)和 mongodb,尝试向我的数据添加地理空间索引
我收藏的记录有经度和纬度字段。据我了解,为了创建地理空间索引,您需要一个与 geoJson 点格式相对应的位置字段(https://medium.com/@AbbasPlusPlus/geospatial-data-in-mongodb-storage-indexing-and-queries -1c6db21b7970)。因此,插入数据后我运行:
async function addLocation() {
const res = collection.updateMany(
{ location: { $exists: false } }, // filter
{
$set: {
location: {
type: "Point",
coordinates: ["$longitude", "$latitude"],
},
},
}
);
}
接下来,我尝试添加地理空间索引:
async function createIndex() {
try {
await collection.createIndex({ location: "2dsphere" });
console.log('Index created');
} catch (err) {
console.error(err);
}
}
这给出了标题中的错误
查看记录,我看到了屏幕截图。我认为长纬度字段应该有数字而不是运算符。
如何修复 addLocation,以便它在 long、lat 字段中输入正确的值?