Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»Mongodb – Get the last record from collection

    Mongodb – Get the last record from collection

    By RahulJuly 30, 20221 Min Read

    MongoDB find() method is used to select documents from a specified collection. It also set the cursor position to the selected document.

    Advertisement

    The default find() method gets the documents from the start of the collection. In some situations, we need to fetch the last record from a collection. Use the following query to get the last records from a MongoDB collection.

    db.collection.find().limit(1).sort({$natural:-1}) 
    

    The above query provides the single last record only, You can also fetch the multiple records from the end of the MongoDB collection.

    You can set the number of records with limit() method as below:

    db.collection.find().limit(5).sort({$natural:-1}) 
    

    Thanks, Hope this quick how-to tutorial helps you to fulfill your requirements.

    collection find method mongodb query sort
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)

    Setting Up Permissions in Home Directory on Linux

    Configuring Apache Userdir on Ubuntu and Debian: A Simplified Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)
    • Understanding Basic Git Workflow: Add, Commit, Push
    • The Difference Between Git Reset –soft, –mixed, and –hard
    • Understanding the Staging Area in Git’s Workflow
    • Python Function with Parameters, Return and Data Types
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.