将查询的结果转化为列表的代码为:
host = 'localhost'
port = 27017
client = MongoClient(host, port)
db = client.database_name
collection = db.collection_name
list_data = list(collection.find({}, {'field1': 1}).sort([('_id', -1)]).limit(1))
这里sort内部是列表+元祖的形式,和平时查询的语句不太一样
如果写成:
list_data = list(collection.find({}, {'field1': 1}).sort({'_id': -1}).limit(1))
会造成以下报错:
TypeError: if no direction is specified, key_or_list must be an instance of list