i am trying to create a api that can fetch recently added 10 records every time the api is called from the data base is there a method to do that
i have this code which can access all the records how i limit only the first 10
app.get("/questapi", function (req, res) {
mydb
.collection("questions")
.find({})
.toArray(function (err, data) {
if (err) throw error;
res.send(data);
});
});
every time i call the api it should get recently added 10 i am fetching from mogodb
Try this,
app.get("/questapi", function (req, res) {
mydb
.collection("questions")
.find()
.limit(10); //here you can limit how many elements you want to retrieve
.toArray(function (err, data) {
if (err) throw error;
res.send(data);
});
});