Q:

I want every nth document from Database MongoDB

this are my collection of about 250k documents:
{ 
    "_id" : ObjectId("5ccfbae47dd9094e30998954"), 
    "date" : "05/02/2019"
}

{ 
    "_id" : ObjectId("5ccfbaff7dd9094e30998956"), 
    "date" : "05/04/2019"
}

{ 
    "_id" : ObjectId("5ccfd7dc6432426a9b4a4785"), 
    "date" : "05/06/2019"
}

{ 
    "_id" : ObjectId("5ccfd8d96432426a9b4a4788"), 
    "date" : "05/08/2019"
}

{ 
    "_id" : ObjectId("5cd01ab6c3bb270aa5c12ade"), 
    "date" : "05/04/2019"
}

{ 
    "_id" : ObjectId("5cd0227abf056b4b82d46ba9"), 
    "date" : "05/06/2019"
}

i have more than 250k documents in my collections. I have to get the every nth data from my collection so it will not take more time.
as Exp:- i want every 2nd Document from collection, and the expected documents are like, 
{ 
    "_id" : ObjectId("5ccfbaff7dd9094e30998956"), 
    "date" : "05/04/2019"
}

{ 
    "_id" : ObjectId("5ccfd8d96432426a9b4a4788"), 
    "date" : "05/08/2019"
}

{ 
    "_id" : ObjectId("5cd0227abf056b4b82d46ba9"), 
    "date" : "05/06/2019"
}

is there any solution for this problem?

A:

You can try as below: 
db.collection.aggregate([
    {
        "$group": { 
            "_id": null,
            data: { $push: "$$ROOT"}
        }     
    },
    {
        $project: {
            requiredResult: {
                $map: {
                    input: { $range: [ 0, { $size: "$data" }, 2 ] },    // here you can replace 2 with any number (N)                        
                    as: "index",
                    in: { $arrayElemAt: [ "$data", "$$index" ] }
                }
            }
        }
    }
])

