簡體   English   中英

如何使用Python在Mongo中提取集合?

[英]How to extract collections in Mongo using Python?

如果我的問題太天真了,請道歉,我是python的新手,我正在嘗試通過pymongo使用集合。 我試圖使用提取名稱

collects = db.collection_names(); #This returns a list with names of collections

但是當我嘗試使用

cursor = db.collects[1].find(); #This returns a cursor which has no reference to a collection. 

我知道上面的代碼使用字符串而不是對象。 因此,我想知道如何才能完成為數據庫中的每個集合保留一個游標的任務,以后可以用來執行搜索和更新等操作。

對不起,我還不能創建評論,但是您嘗試過嗎?:

cursor = db.getCollection(collects[1]).find();

如果您使用的是pymongo驅動程序,則必須使用get_collection方法或dict樣式的查找。 另外,您可能希望在collection_names中將include_system_collections設置為False ,這樣就不會包括系統集合(例如system.indexes)。

import pymongo

client = pymongo.MongoClient()
db = client.db
collects = db.collection_names(include_system_collections=False)
cursor = db.get_collection(collects[1]).find()

要么

cursor = db[collects[1]].find()

暫無
暫無

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

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