簡體   English   中英

db insert命令中的集合名稱的Python / Pymongo變量

[英]Python / Pymongo variable for collection name in db insert command

我有一小段代碼,基本上需要一個列表並運行一個循環,針對twitter運行搜索查詢,列表中的每個項目。 我希望列表中的每個項目都是集合名稱,但由於某種原因,我無法弄清楚如何使db<collection_name_variable>.insert(post)>實際工作:

我收到一個錯誤:

TypeError:+不支持的操作數類型:''數據庫'和'str'

我知道這可能是非常基本但我只是在學習。

from twython import Twython
from pymongo import *

conn = Connection('localhost')
db = conn.nosqltweets
twitter = Twython()

types = ['mongodb', 'cassandra', 'couchdb']

for nosql in types:
    search_results = twitter.searchTwitter(q=nosql, rpp="100")
    for tweet in search_results["results"]:
        from_user = tweet['from_user'].encode('utf-8')
        text = tweet['text']
        created_at = tweet['created_at']
        id_str = tweet['id_str']
        post = { 'id_str': id_str, 'from_user': from_user, 'created_at': created_at }
        insert = db + nosql + ".insert(post)"
        insert

更換:

insert = db + nosql + ".insert(post)"
insert

有:

db[nosql].insert(post)

暫無
暫無

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

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