【发布时间】:2018-08-17 12:17:19
【问题描述】:
我使用scrapy爬取数据并将其保存到mongodb,我想将2dsphere索引保存在mongodb中。
这是我的带有 scrapy 的 pipelines.py 文件
from pymongo import MongoClient
from scrapy.conf import settings
class MongoDBPipeline(object):
global theaters
theaters = []
def __init__(self):
connection = MongoClient(
settings['MONGODB_SERVER'],
settings['MONGODB_PORT'])
self.db = connection[settings['MONGODB_DB']]
self.collection = self.db[settings['MONGODB_COLLECTION']]
def open_spider(self, spider):
print 'Pipelines => open_spider =>'
def process_item(self, item, spider):
global theaters
# get the class item name to be collection name
self.collection = self.db[type(item).__name__.replace('_Item','')]
if item['theater'] not in theaters:
print 'remove=>',item['theater']
theaters.append(item['theater'])
self.collection.remove({'theater': item['theater']})
# insert the collection name that is from class object item
self.collection.insert(dict(item))
# Here is what i try to create 2dsphere index
self.collection.create_index({"location": "2dsphere"})
return item
当我使用self.collection.create_index({"location": "2dsphere"})时
显示错误TypeError: if no direction is specified, key_or_list must be an instance of list
如果我尝试
self.collection.create_index([('location', "2dsphere")], name='search_index', default_language='english')
没有错误了,但是我的mongodb在location下仍然没有任何索引。
我认为我遵循 GeoJson 格式。
当我使用 scrapy 时,有什么方法可以在 mongodb 中保存 2dsphere 索引?或者我应该像照片结构一样保存数据并通过另一个服务器文件保存索引(如nodejs)
任何帮助将不胜感激。提前致谢。
根据Adam Harrison回复,我尝试将我的mongodb名称location更改为geometry
比在我的 pipelines.py 文件中添加代码 import pymongo
并使用self.collection.create_index([("geometry", pymongo.GEOSPHERE)])
【问题讨论】:
-
他的问题是如何在 pymongo 中使用
2dsphere索引而不是如何在 pymongo 中保存2dsphere。 -
检查答案,它解决了如何指定
2dsphere索引。 -
试试
collection.create_index([("geometry", pymongo.GEOSPHERE)])。取自票 bro-grammer 链接。请参阅api.mongodb.com/python/current/api/pymongo/collection.html 了解更多信息 -
感谢您的回复,我添加
import pymongo并将密钥名称location更改为geometry然后我使用self.collection.create_index([("geometry", pymongo.GEOSPHERE)])。我的 mongodb 上仍然没有错误信息和索引。
标签: python node.js mongodb scrapy 2dsphere