为MongoDB创建索引
使用MongoDb和Pymongo,我一直在尝试在数据库中创建MyLocations字段的2d索引。我遵循了有关如何使geoJSON对象创建DB集合“位置”项的指南。这样做的原因是,我可以对数据库中的条目进行“近似”搜索,并返回一个位置列表,就像在谷歌上搜索咖啡馆一样,该区域内会出现大量标记 我的数据库条目如下所示:为MongoDB创建索引,mongodb,pymongo,Mongodb,Pymongo,使用MongoDb和Pymongo,我一直在尝试在数据库中创建MyLocations字段的2d索引。我遵循了有关如何使geoJSON对象创建DB集合“位置”项的指南。这样做的原因是,我可以对数据库中的条目进行“近似”搜索,并返回一个位置列表,就像在谷歌上搜索咖啡馆一样,该区域内会出现大量标记 我的数据库条目如下所示: new_location = { "name": <name>, "descriptio
new_location = {
"name": <name>,
"description": <descript>,
"rating": <rating>,
"location": {"type": "FeatureCollection", "features" :[ {
"type" : "Feature","geometry" :{
"type" : "Point", "coordinates" : [lng, lat]}, "properties" : ""}]},
"file": <file>,
"posted_by": session["user"]
}
mongo.db.locations.insert_one(new_location)
无论我做什么,我总是会发现数组格式不正确的错误
pymongo.errors.OperationFailure:索引生成失败:890e39f7-f09a-45e9-beed-1b879be03285:集合60870b6f802c945b786e98a9_wildomping.locations(02746b9f-e586-488e-9d24-ce9c882cf539):由::位置对象引起,位置数组格式不正确,完整错误:{'operationTime':时间戳(1620766990,9),'ok':0.0,'errmsg':'Index build failed:890e39f7-f09a-45e9-beed-1b879be03285:Collection 60870b6f802c945b786e98a9_wildomping.locations(02746b9f-e586-488e-9d24-ce9c882cf539):由::位置对象引起,位置数组格式不正确,'code':16804,'code':'Location16804','clusterTime':{'clusterTime':时间戳(1620766990,9),“签名”:{“散列”:b“C\xc3/\x1bo!I.\xec\x18z6i'\xc4\xa6)\xe9\x86\xff”,“密钥ID”:6950140143088959491}
在这个阶段,我想弄清楚这件事,但我的眼睛却变得模糊了。任何帮助都将不胜感激。
Feature
和FeatureCollection
不在MongoDB支持列表中。Ye我曾尝试创建一个符合GeoJSON对象文档中提到的所需格式的GeoJSON对象,但它就是不起作用。我最终开始创建遗留坐标的2dsphere索引
mongo.db.locations.create_index([("locations.location", "2dsphere")])
c = mongo.db.locations.find({ "locations.location" :
{"$near" :
{"$geometry" :
{ "type" : "Point" ,
"coordinates" : [ -6.86 , 52.2 ] } ,
"$maxDistance ": 100000 }}})
此外,我的收藏插页的结构改回了我原来的更简单(传统坐标)形式
谢谢你的帮助 如果以这种方式嵌套,则无法索引。我建议您创建一个单独的“feature”集合,其中包含自己的几何体
mongo.db.locations.create_index([("locations.location", "2dsphere")])
c = mongo.db.locations.find({ "locations.location" :
{"$near" :
{"$geometry" :
{ "type" : "Point" ,
"coordinates" : [ -6.86 , 52.2 ] } ,
"$maxDistance ": 100000 }}})
"name": request.form.get("location_name"),
"description": request.form.get("location_description"),
"rating": request.form.get("rating"),
"location": [lng, lat],
"file": request.form.get("file"),
"posted_by": session["user"]
}