How do I turn MongoDB query into a JSON?

后端 未结 4 2025
难免孤独
难免孤独 2021-01-30 08:54
for p in db.collection.find({\"test_set\":\"abc\"}):
    posts.append(p)
thejson = json.dumps({\'results\':posts})
return  HttpResponse(thejson, mimetype=\"application/j         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 09:45

    The json module won't work due to things like the ObjectID.

    Luckily PyMongo provides json_util which ...

    ... allow[s] for specialized encoding and decoding of BSON documents into Mongo Extended JSON's Strict mode. This lets you encode / decode BSON documents to JSON even when they use special BSON types.

提交回复
热议问题