Skip to content

Instantly share code, notes, and snippets.

@w3irdrobot
Created September 22, 2019 01:34
Show Gist options
  • Save w3irdrobot/1f73c20d6018b4bf15a85b4445e81dc0 to your computer and use it in GitHub Desktop.
Save w3irdrobot/1f73c20d6018b4bf15a85b4445e81dc0 to your computer and use it in GitHub Desktop.
Get last in Mongo collection and return partial object
db.getCollection('collectionName').find().sort({"$natural": -1}).limit(1).map(function(doc) {
var obj = {}
obj["_id"] = doc._id
obj["another_field"] = doc.another_field
return obj
})[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment