What is most efficient way to get the last record in a large MongoDB collection

in searching on this topic I found use cases combining the skip(), sort() and count() methods with the find(), as well as aggregation.

What I would like to know is with a large dataset, which method is most efficient in retrieving the last record, especially when it needs to be done often.

What does your data look like? What’s the field that determines the “last” record? Last inserted or last by time field? What indexes do you have?

If inserting via the driver, the driver can generate the _id field so if you have multiple processes on different machines inserting data then the _id may not guarantee insertion order over all data depending on OS Clocks.

https://www.mongodb.com/docs/manual/core/document/#:~:text=In%20MongoDB%2C%20each%20document%20stored,ObjectId%20for%20the%20_id%20field.

As a very basic, if you had one process inserting data then sorting by ID desc and pulling one record could result in making use of the auto-generated index on _id and be a very quick lookup:

db.getCollection("Test").explain().aggregate([
{
    $sort:{
        _id:-1
    }
},
{
    $limit:1
}
])
        "winningPlan" : {
            "stage" : "LIMIT",
            "limitAmount" : 1.0,
            "inputStage" : {
                "stage" : "FETCH",
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "_id" : 1.0
                    },
                    "indexName" : "_id_",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "_id" : [

                        ]
                    },
                    "isUnique" : true,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 2.0,
                    "direction" : "backward",
                    "indexBounds" : {
                        "_id" : [
                            "[MaxKey, MinKey]"
                        ]
                    }
                }
            }
        },

image

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.