简体   繁体   English

在pymongo中选择*在MongoDB中限制100个等效项

[英]select * limit 100 equivalent in MongoDB using pymongo

I am very new to MongoDB and using jupyter notebook for extracting data from mongodb. 我对MongoDB很陌生,并使用jupyter notebook从mongodb中提取数据。 I am trying to fetch first 100 documents in MongoDB and i do have a crude way of fetching only 100 documents which is to add a counter and stop at 100th counter. 我试图在MongoDB中获取前100个文档,但确实有一种仅获取100个文档的粗略方法,即添加一个计数器并在第100个计数器处停止。

#import library
import pymongo
from pymongo import MongoClient

#connect with mongo client
client = MongoClient('myipaddress', 27011)

db = client.mydatabase_test
collection = db.mycollection_in_testdatabase

#start counter
i=0
for obj in collection.find():
    if i <= 100:
        print obj['dummy_column']
        i = i+1
    else:
        break

Is there any better way to do this in mongodb? 在mongodb中还有更好的方法吗? I am sure there must be some equivalent of select * from mydb limit 100 in mongodb. 我确定在mongodb中必须存在select * from mydb limit 100中的select * from mydb limit 100等效的内容。 Can anyone help? 有人可以帮忙吗?

As Yogesh said , you should use limit . 正如Yogesh所说,您应该使用limit
eg 例如

cursor = collection.find().limit(100)

Now that you have created cursor you can extract some field like this: 现在,您已经创建了游标,您可以提取一些字段,如下所示:

something = []  # list for storing your 100 values of field dummy_column

for doc in cursor:   # loop through these 100 entries 

    something.append(doc.get('dummy_column', '')) # append to this list vallue of field **dummy_column**

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

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