Pymongo/bson: Convert python.cursor.Cursor object to serializable/JSON object

后端 未结 3 1255
臣服心动
臣服心动 2021-02-05 06:14

New to MongoDb and Python (webapp2). So, I was fetching some data from a mongodb database. But I was unable to use json.dumps on the r

3条回答
  •  鱼传尺愫
    2021-02-05 06:27

    Use dumps from bson.json_util:

    >>> c = pymongo.MongoClient()
    >>> c.test.test.count()
    5
    >>> from bson.json_util import dumps
    >>> dumps(c.test.test.find())
    '[{"_id": {"$oid": "555cb3a7fa5bd85b81d5a624"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a625"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a626"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a627"}}, {"_id": {"$oid": "555cb3a7fa5bd85b81d5a628"}}]'
    

提交回复
热议问题