简体   繁体   English

如何在PyMongo中选择所有数据?

[英]How to select all data in PyMongo?

I want to select all data or select with conditional in table random but I can't find any guide in MongoDB in Python to do this. 我想选择所有数据或在表中选择有条件的random表,但是我无法在Python的MongoDB中找到任何指南来做到这一点。

And I can't show all data was select. 而且我不能证明所有数据都是精选的。

Here my code: 这是我的代码:

def mongoSelectStatement(result_queue):
    client = MongoClient('mongodb://localhost:27017')
    db = client.random

    cursor = db.random.find({"gia_tri": "0.5748676522161966"})
    # cursor = db.random.find()
    inserted_documents_count = cursor.count()

    for document in cursor:
        result_queue.put(document)

There is a quite comprehensive documentation for mongodb. 关于mongodb的文档非常全面。 For python (Pymongo) here is the URL: https://api.mongodb.org/python/current/ 对于python(pymongo),这是URL: https ://api.mongodb.org/python/current/

Note: Consider the version you are running. 注意:考虑您正在运行的版本。 Since the latest version has new features and functions. 由于最新版本具有新的特性和功能。

To verify pymongo version you are using execute the following: 要验证您使用的pymongo版本,请执行以下操作:

import pymongo
pymongo.version

Now. 现在。 Regarding the select query you asked for. 关于您要求的选择查询。 As far as I can tell the code you presented is fine. 据我所知,您提供的代码很好。 Here is the select structure in mongodb. 这是mongodb中的选择结构。

First off it is called find() . 首先,它称为find()

In pymongo; 在pymongo中 if you want to select specific rows( not really rows in mongodb they are called documents. I am saying rows to make it easy to understand. I am assuming you are comparing mongodb to SQL); 如果您想选择特定的行(在mongodb中不是真正的 ,它们被称为文档。我说的是行以便于理解。我假设您正在将mongodb与SQL进行比较); alright so If you want to select specific document from the table (called collection in mongodb) use the following structure (I will use random as collection name; also assuming that the random table has the following attributes: age:10, type:ninja, class:black, level:1903 ): 好吧,因此,如果您要从表中选择特定文档(在mongodb中称为集合 ),请使用以下结构(我将使用random作为集合名称;还要假设随机表具有以下属性: age:10,type:ninja,等级:黑色,等级:1903 ):

db.random.find({ "age":"10" }) This will return all documents that have age 10 in them. db.random.find({ "age":"10" })这将返回所有年龄为10的文档。

you could add more conditions simply by separating with commas 您可以简单地用逗号分隔来添加更多条件

db.random.find({ "age":"10", "type":"ninja" }) This will select all data with age 10 and type ninja. db.random.find({ "age":"10", "type":"ninja" })这将选择所有10岁以下的数据并键入ninja。

if you want to get all data just leave empty as: 如果要获取所有数据,请保留为空:

db.random.find({})

Now the previous examples display everything (age, type, class, level and _id). 现在,前面的示例将显示所有内容(年龄,类型,类,级别和_id)。 If you want to display specific attributes say only the age you will have to add another argument to find called projection eg: (1 is show, 0 is do not show): 如果要显示特定属性,只需说出年龄,您就必须添加另一个参数来找到称为投影的参数,例如:(1是显示,0是不显示):

{'age':1} 

Note here that this returns age as well as _id . 注意这里返回的是age_id _id is always returned by default. 默认情况下始终返回_id。 You have to explicitly tell it not to returning it as: 您必须明确告诉它不要将其返回为:

db.random.find({ "age":"10", "name":"ninja"  },  {"age":1, "_id":0} )

I hope that could get you started. 我希望这可以帮助您入门。 Take a look at the documentation is very thorough. 看看文档非常详尽。

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

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