How to sort and limit the returned query results in mongoose

1 · Adrian Matei · Feb. 3, 2021, 11 p.m.
Project: codever - File: personal-bookmarks.service.js Use the sort and limit methods. In the following example we receive the latest 30 (limit) created bookmarks (sort descending by createdAt date) : let getLastCreatedBookmarks = async (userId) => { const bookmarks = await Bookmark.find({userId: userId}) .sort({createdAt: -1}) // -1 for descending sort .limit(30); return bookmarks; }; Reference - https://docs.mongodb.com/manual/reference/method/cursor.sort/ Shared...