簡體   English   中英

Pymongo:將數據插入變量集合

[英]Pymongo : Insert data to a variable collection

想象這是我的課程,我想將myObject插入到集合中。 后者的名稱是可變的,並且取決於配置對象。

我嘗試了這個:

class MyClass(Object):

    def __init__(self, config):

        self.config = config
        self.db = config.db
        self.collection = config.db


    def on_data(self, myObject):
        self.db.self.collection.insert(myObject)

和這個 :

class MyClass(tObject):

    def __init__(self, config):

        self.config = config

        self.db = config.db       
        self.collection = config.db


    def on_data(self, myObject):
        self.db[self.collection].insert(myObject)

這些都沒有工作!

我也嘗試過這個:

class MyClass(tweepy.Object):

    def __init__(self, config):

        self.config = config
        self.connection = config.connection
        self.db = config.db
        self.collection = config.db


    def on_data(self, myObject):
        self.connection[self.db][self.collection].insert(myObject)

我有這個錯誤:

TypeError: name must be an instance of basestrin

知道config是DBConfig的實例:

class DBConfig():
    def __init__(self, db, collection):
        self.connection = Connection()
        self.db = self.connection[db]
        self.collection = self.db[collection]

我該如何解決?

看起來您的示例課中有一些錯別字。 即,您將config.db定義了self.dbself.collection

class MyClass(tweepy.Object):

    def __init__(self, config):
        self.config = config
        self.db = config.db
        # define `self.collection` appropriately
        self.collection = config.collection

    def on_data(self, tweet):
        # just use the collections insert method
        self.collection.insert(myObject)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM