无法在python中迭代mongo对象

时间:2018-09-19 12:42:18

标签: python mongodb

我试图获取记录(书籍),这些记录在Mongo DB集合“书籍”中提供。我正在用pymongo和flask。

下面是我的代码。在代码中,如果我删除查询更新行(query.update({'author': 'sachin'})),它将正常工作。

更新查询字典时出什么问题?

from pymongo import connection as pymongo_client
import urllib


def get_books(query, skip_val=0, limit=None):
    conn, client = _connect()
    if limit:
        result = client.books.find(query).skip(skip_val).limit(limit)
    else:
        result = client.books.find(query)
    conn.close()
    return result


def _connect():
    user = "root"
    password = "password"
    host = "172.17.1.14"
    port = "27017"
    db_name = "books"
    auth_database = "admin"
    if user and password and auth_database:
        uri = "mongodb://%s:%s@%s:%s/%s" % (
            user, urllib.quote_plus(password), host, port, auth_database)
    else:
        uri = "mongodb://%s:%s" % (host, port)
    conn = pymongo_client.Connection(uri, j=True)
    db = conn[db_name]
    return conn, db

query = {'project_name': 'cricket'}

books = get_books(query)

query.update({'author': 'sachin'})

for book in books:
    print book

0 个答案:

没有答案