1

sourcesというテーブルを持つ MySQL データベースがあり、srctypeurlという 2 つの列があり、srctype は名前 (例: Hello) で、url は URL (例: http://google.com )です。

SQLAlchemy を使用する Python では、srctype をフィルター処理して、URL のリストを取得できます。

src = "hello"    
links = session.query(sources).filter_by(srctype=src).all()

簡単です。今、このデータを MongoDB に移行しています。これにはpymongoを使用しています。

srctype と url を mongodb のデータベースに保存する機能があります

    def insertSources(self, srctype, link):
        """ Insert rss sources so we can parse them """
        new = {srctype: link}
        self.__sources.insert(new)

および srctype を取得する関数

 def getSources(self, type=None, single=True): # type == srctype
    if type:
        if single:
            return self.__sources.find_one()
        else:
            return iter(self.__sources.find({srctype:type}))
    else:
        if single:
            return self.__sources.find_one()
        else:
            return iter(self.__sources.find())

ここでの問題は、srctype という列も url という列もないため、SQLAlchemy/MySQL の例と同じようにするにはどうすればよいでしょうか?

MySQL では次のようになります。

SELECT * FROM sources WHERE srctype="hello"

私が持っている検索機能でそれがどのようになるのだろうか(挿入機能でも、私がしたことが私が望む仕事に対して正しいかどうかわからないので)。insertSources関数では、MongoDB にdictを追加するだけです。明らかに、MongoDB の srctype は列ではないため、getSources 関数で srctype を取得することはできません。どんな助けでも素晴らしいでしょう。

4

1 に答える 1

2

問題は、データを保存するときに名前を正しく設定していないことです。それ以外の

def insertSources(self, srctype, link):
    """ Insert rss sources so we can parse them """
    new = {srctype: link}
    self.__sources.insert(new)

やったほうがいい

def insertSources(self, srctype, link):
    """ Insert rss sources so we can parse them """
    new = {'srctype': srctype, 'link': link}
    self.__sources.insert(new)

同様にgetSources(). srctypeが渡された場合find()find_one()を受け取る必要があります{'srctype': type}

于 2012-11-02T21:36:51.680 に答える