8

Suppose I specify a MongoDB cursor with pymongo, which DOES NOT include all fields in the result set like this:

from pymongo import MongoClient
conn = MongoClient('mongodb://localhost:27017')
cur = conn['my_db']['my_collection'].find({},{'_id' : 0, 'my_unwanted_field' : 0})

Is there a function or attribute that will return me the names of the fields present in cur.

Something equivalent on Mongo Shell using findOne would be:

> var cur = findOne({},{'_id' : 0, 'my_unwanted_field' : 0})
> Object.keys(cur)

["field_1", ... , "field_n"]
Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
Souradeep
  • 329
  • 1
  • 5
  • 18
  • @BlakesSeven I don't think the question you mentioned is related to pymongo or MongoDB – Souradeep Mar 12 '16 at 04:30
  • Of course it isn't about pymongo specifically. The object returned is a regular **python** object. Therefore your "question" is not related specifically to pymongo. – Blakes Seven Mar 12 '16 at 04:34
  • @BlakesSeven I'm sorry if I'm not getting you clearly. My original question is about getting field names from a `pymongo.cursor.Cursor` object, and not a 'regular' python object. And in any case, I don't think C# will help me because I am not familiar with that language, yet. – Souradeep Mar 12 '16 at 04:39
  • The result of `.find_one()` is not a cursor. Anything returned when you iterate a cursor ( should you have used `.find()` instead) is also just a plain object. You don't get keys from a cursor. – Blakes Seven Mar 12 '16 at 04:40
  • Yes, maybe I should mention that in my question. This was more of a trivia for me. I could have just gone one iteration of `cur` and extracted the keys of the `dict`. However, I am trying to look out if I could do the same thing, without the method I just mentioned, or using the `find_one` method. – Souradeep Mar 12 '16 at 04:43
  • It's a dict/object/hash ( whatever floats your boat ) and the linked duplicate is how you do what you asked in python. Nothing pymongo or MongoDB related in any way. Which is why your title and tags are changed. Just do what what the answered on the linked question tell you to do. – Blakes Seven Mar 12 '16 at 04:45
  • Jeez `Object.keys()` is **JavaScript** and has nothing again to do with MongoDB. Every single MongoDB driver returns a basic object form, and generally the common supported one in languages that have this. Like python. – Blakes Seven Mar 12 '16 at 04:47
  • @Souradeep - if you expect a cursor to have information about fields in the collection's documents, then you shouldn't be using a schema-less db like Mongo. Since the collection's documents are completely unconstrained as to the fields they define or do not define, there really is no concept of getting the keys for the docs, except for iterating over each doc and getting its keys() separately. And even if you do iterate to the first dict and extract its keys, there is no guarantee that the remaining docs will have the same. – PaulMcG Mar 12 '16 at 06:13

1 Answers1

2

Use a loop to iterate the cursor object, then use .keys() to get the keys of any regular python dict, which seems like this:

for item in cur:
    print item.keys()
piglei
  • 1,188
  • 10
  • 13
  • That would be correct for the result of a [`.find()`](http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find) operation which returns a [Cursor](http://api.mongodb.org/python/current/api/pymongo/cursor.html) object. But the question posted asks for keys from [`.find_one()`](http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one) which of course just returns a standard dict. Does not really add to what is supplied in the duplicate anyway. – Blakes Seven Mar 12 '16 at 23:23