2013-10-29 138 views

回答

0

確定它可能在(子類)MongoDBPipeline管道中。

以下是未經測試,但一個選項是將self.collection更改爲集合字典,將項目類型映射到Mongo集合。

class CustomMongoDBPipeleine(MongoDBPipeline): 

    def __init__(self, settings): 
    ... 
    mapping = { 
     PersonItem: 'persons', 
     BookItem: 'books', 
    } 
    self.collection = {} 
    for itype, collection_name in mapping.items(): 
     self.collection[itype] = database[collection_name] 

映射可能來自配置,並且使用項類型類名直接替代項類。

而且使用這樣的:

insert_item(self, item, spider): 
     ... 
     self.collection.get(type(item)).insert(item, continue_on_error=True) 
     ... 
0

您還可以創建兩個不同的管道對每一種類型:在settings.py

class PersonPipeline(object): 
    def __init__(self): 
     connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT']) 
     db = connection[settings['MONGODB_DB']] 
     self.collection = db[settings['MONGODB_PERSON_COLLECTION']] # use the person collection 

    def process_item(self, item, spider): 
     if not isinstance(item,PersonItem): 
      return item # return the item to let other pipeline to handle it 
     self.collection.insert(dict(item)) 

class BookPipeline(object): 
    def __init__(self): 
     connection = pymongo.Connection(settings['MONGODB_SERVER'], settings['MONGODB_PORT']) 
     db = connection[settings['MONGODB_DB']] 
     self.collection = db[settings['MONGODB_BOOK_COLLECTION']] # use the book collection 

    def process_item(self, item, spider): 
     if not isinstance(item,PersonItem): 
      return item # return the item to let other pipeline to handle it 
     self.collection.insert(dict(item)) 

:在pipelines.py

ITEM_PIPELINES = { # declare the handle sequence 
    'myproject.pipelines.PersonPipeline':100, 
    'myproject.pipelines.BookPipeline':200, 
} 
MONGODB_SERVER = "localhost" 
MONGODB_PORT = 27017 
MONGODB_DB = "MyDB" # db name 
MONGODB_PLACE_COLLECTION = "persons" # collection name 
MONGODB_LIST_COLLECTION = "books" 

當一件物品返回時,PersonPipeline無線我會先處理它。如果該項目不是PersonItem類型,則它將返回到下一個管道,在此情況下爲BookPipeline

相關問題