python - 使用 Pymongo 在 MongoDB 中提取最新文档的时间戳

标签 python mongodb pymongo

我有 mongoDB collection带列filenametext
我想提取最新 document 的时间戳使用 pymongo
我试过的:

from pymongo import MongoClient
host = "127.0.0.1:27017"
client = MongoClient(host)

# print(client)

# Getting a database
db = client['ResumeParsing']
# Getting a collection
coll = db.Resume

print(coll.find({"_id": {"$gt": 1}}).sort([("_id", 1), ("date", -1)]))

这给了我输出:
<pymongo.cursor.Cursor object at 0x00000187738CD860>

虽然我想要日期时间作为结果。

最佳答案

我找到了解决办法。

from pymongo import MongoClient
import pymongo

host = "127.0.0.1:27017"
client = MongoClient(host)

db = client['ResumeParsing']

coll = db.Resume

# Convert the output of query into list 
latest_doc = list(db.Resume.find().sort("_id", pymongo.DESCENDING).limit(1))

# use generation_time attribute to get datetime from _id
print(latest_doc[0]['_id'].generation_time)

这给了我输出:
2018-08-27 09:16:56+00:00

关于python - 使用 Pymongo 在 MongoDB 中提取最新文档的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037219/

相关文章:

node.js - Mongoose 转换为 ObjectId 值失败

java - Json MongoDb 查询中的 Spring-boot 可选 ArrayList 参数

mongodb - 如何在MongoDB Atlas中获取 'full database name'?

python - 没有名为 mem_profile 的模块

python - 类型错误 : cannot concatenate 'str' and 'UUID' objects

python - 如何迭代文件并将所有 id 值放入列表中 [Python]

python - 在 Python 中,如何使用列表中的字符串来调用同名函数?

mongodb - 无法连接到 MongoLab "auth fails"错误

python - 使用 PyMongo 获取按发布日期降序排列的不同值

python - Pymongo - 删除没有特定属性的数据库元组