In the first snippet, the object you assign to the variable queryset
is a (single) document (find_one
returns a document). I.e. a document is fetched from the DB, and stored in memory. A document is json-serializable, so it works.
In the second snippet, the object you assign to queryset
is a Cursor
. The resulting documents haven't been fetched yet, they are only fetched when you iterate over the cursor. Cursor is not json-serializable, because it does not represent the documents, but a generator of the resulting documents. So what you can do is to first fetch all the documents, and then serialize the list of documents:
>>> queryset = FIRM.find({'_id':ObjectId("52e56c009dbc794999ea5c3d")},{'wholesalers.name':1,'wholesalers.id':1,'_id':0})
>>> docs = list(queryset) # fetch the documents
>>> simplejson.dumps(documents, default = json_util.default)