फिर भी MongoDB संग्रह से अंतिम आइटम प्राप्त करने का एक और तरीका (उदाहरणों के बारे में बुरा मत मानना):
> db.collection.find().sort({'_id':-1}).limit(1)
सामान्य प्रोजेक्शन
> db.Sports.find()
{ "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2017", "Body" : "Again, the Pats won the Super Bowl." }
{ "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." }
{ "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
क्रमबद्ध प्रोजेक्शन (_id: रिवर्स ऑर्डर)
> db.Sports.find().sort({'_id':-1})
{ "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
{ "_id" : ObjectId("5bfb6011dea65504b456ab13"), "Type" : "World Cup 2018", "Head" : "Brazil Qualified for Round of 16", "Body" : "The Brazilians are happy today, due to the qualification of the Brazilian Team for the Round of 16 for the World Cup 2018." }
{ "_id" : ObjectId("5bfb5f82dea65504b456ab12"), "Type" : "NFL", "Head" : "Patriots Won SuperBowl 2018", "Body" : "Again, the Pats won the Super Bowl" }
sort({'_id':-1})
, सभी दस्तावेजों के अवरोही क्रम में एक प्रक्षेपण को परिभाषित करता है, जो उनके _id
एस के आधार पर होता है ।
क्रमबद्ध प्रोजेक्शन (_id: रिवर्स ऑर्डर): एक संग्रह से नवीनतम (अंतिम) दस्तावेज़ प्राप्त करना।
> db.Sports.find().sort({'_id':-1}).limit(1)
{ "_id" : ObjectId("5bfb60b1dea65504b456ab14"), "Type" : "F1", "Head" : "Ferrari Lost Championship", "Body" : "By two positions, Ferrari loses the F1 Championship, leaving the Italians in tears." }
db.collection.find().limit(1).sort({$natural:-1}).pretty()
यदि आप चाहते हैं कि यह अच्छा