简体   繁体   English

通过使用PyMongo进行迭代时,如何在集合中找到值的名称(键)?

[英]How do I find the name (key) of values in a collection while it's being iterated through using PyMongo?

When using a cursor to iterate through a collection like so, 当使用游标像这样遍历集合时,

import pymongo
cursor = pymongo.Connection().test_db.test_collection.find()
for item in cursor:
    print item
    #this will print the item as a dictionary

only the values are returned. 仅返回值。 I'm looking for a way to access the name of the value. 我正在寻找一种访问值名称的方法。

I have found a similar method called getCollectionNames() however it is used to return every collection name in the database. 我发现了一个类似的名为getCollectionNames()的方法,但是它用于返回数据库中的每个集合名称。


Quick [Hacky] Fix: Store the name of the collection inside the collection itself and simple call the name from the value returned by the cursor using item.name 快速[Hacky]修复:将集合名称存储在集合本身内部,并使用item.name简单地从光标返回的值中调用名称。

If I understand correctly, you are looking for a way to get the collection name you are working on or that you have queried; 如果我理解正确,那么您正在寻找一种方法来获取您正在使用或已查询的集合名称; If that is the case here is how you do it: user collection.name 如果是这样,请按照以下步骤操作:user collection.name

import pymongo
cursor = pymongo.Connection().test_db.test_collection.find()
print cursor.collection.name
for item in cursor:
    print item
    #this will print the item as a dictionary

If you mean to retrieve the keys inside of the collection itself. 如果您要检索集合本身内部的键。 You can simply add an extra loop and retrieve the keys for each document in cursor. 您可以简单地添加一个额外的循环并检索光标中每个文档的键。 example: 例:

import pymongo
cursor = pymongo.Connection().test_db.test_collection.find()
print cursor.collection.name
for item in cursor:
    for key in item:
        print key

You can iterate over the top level field names and their associated values using this snippet: 您可以使用以下代码片段访问顶级字段名称及其关联的值:

for fieldname, fieldvalue in item.iteritems():
   print (fieldname, fieldvalue )

暂无
暂无

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

相关问题 我如何找到 mongodb 集合并使用 pymongo 更新多个字段 - How do i find mongodb collection and update multiple fields using pymongo 如何在pymongo中查找特定键的所有值? - How to find all values for a specific key in pymongo? 如何使用 pymongo 获取仅包含 ObjectId 的列表? - How do I get a list of just the ObjectId's using pymongo? 如何使用PyMongo将索引从一个集合复制到另一个集合? - How do I copy the indexes from one collection to another collection using PyMongo? 我有一本字典,其中的值本身也是一本字典——如何将值写入 CSV,值中的键是列名? - I have a dictionary & the values are also a dictionary itself- how do I write the values to a CSV, with the key in the values being the column name? 如何遍历字典的一组值并返回与给定组匹配的一组值的键? - How do I iterate through a dictionary's sets of values and return the key of the sets of values that matches a given set? 如何使用pymongo更新集合中的所有文档 - How do I update all documents in a collection with pymongo 如何使用 pymongo 连接到现有的文档集合/数据库? - How do I use pymongo to connect to an existing document collection/db? 如何在PyMongo中执行等效的SQL Join? 或更具体地,用BSON代码调用Pymongo集合对象? - How do I perform the SQL Join equivalent in PyMongo? Or more specific call a Pymongo collection object in BSON code? 可以使用pymongo将变量用于集合名称吗? - Is it possible to use Variable for collection name using pymongo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM