简体   繁体   English

Python Mongodb简单查询

[英]Python Mongodb simple query

I am new to python and mongo db, i have a dict called data and i wanna use the keys to query the database called db. 我是python和mongo db的新手,我有一个叫做data的字典,我想使用这些键来查询名为db的数据库。

def update_db(data, db):
for key in data:
    x=db.find({'label':key})
    for a in x:
        print a

I get the error message that find() does not exist. 我收到错误消息find()不存在。

Anyone can give me input on my problem ? 任何人都可以就我的问题提供意见吗?

There is no find method in database object. 数据库对象中没有find方法。 You should search documents in some collection, not in database. 您应该搜索某些集合中的文档,而不是数据库中的文档。 Database has collections like SQL database has tables. 数据库具有类似SQL数据库具有表的集合。 Collections have documents, like SQL tables have rows of data. 集合具有文档,例如SQL表具有数据行。 Eg if you have users collection: 例如,如果您有用户集合:

def update_db(data, db):
    for key in data:
         users = db.users
         matchedUsers = users.find({'label':key})
         for user in matchedUsers:
             print user

Future reading PyMongo tutorial 未来阅读PyMongo教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM