- SQL 与 NoSQL
- mongoDB 安装与使用
- mongoDB Web 管理
- pymongo 使用
使用非关系数据库mongoDB
##
pymongo
Example
import pymongo
from pymongo import IndexModel, ASCENDING
class MongoDBPipeline(object):
def __init__(self):
clinet = pymongo.MongoClient("localhost", 27017)
db = clinet["Hub"]
self.PhRes = db["PhRes"]
idx = IndexModel([('link_url', ASCENDING)], unique=True)
self.PhRes.create_indexes([idx])
# if your existing DB has duplicate records, refer to:
# https://stackoverflow.com/questions/35707496/remove-duplicate-in-mongodb/35711737
def process_item(self, item, spider):
print 'MongoDBItem', item
""" 判断类型 存入 MongoDB """
if isinstance(item, VideoItem):
print 'VideoItem True'
try:
self.PhRes.update_one({'link_url': item['link_url']}, {'$set': dict(item)}, upsert=True)
except Exception:
pass
return item