Reputation: 13426
I have mongoDB collection
with columns filename
and text
I want to extract timestamp of latest document
using pymongo
What I Tried:
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)]))
Which is giving me output as :
<pymongo.cursor.Cursor object at 0x00000187738CD860>
While I want datetime as a result.
Upvotes: 5
Views: 1899
Reputation: 13426
I find out the solution.
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)
Which is giving me output as:
2018-08-27 09:16:56+00:00
Upvotes: 7