How to use Fields parameter with Mongoose paginate
See original GitHub issueCurrent Behaviour
The following fields parameter in my Mongoose query is not recognised by paginate function since the query simply returns the entire result including “bids.bid”, which I am explicitly excluding. It appears any fields I include are disregarded.
Additionally, whenever I include a “fields” parameter in the paginate function, the _id is also never returned; however, removing the “fields” parameter entirely brings back the _id field as expected.
List.paginate({
query: query,
limit: paginationLimit,
fields: {
"bids.bid": 0
}
}).then((result) => {
sendJsonResponse(res, 200, result);
}).catch((err) => {
sendJsonResponse(res, 404, err);
});
Expected Behaviour
The documentation for the paginate function refers to the find section so I was expecting the “fields” parameter to work as it does when not using Mongoose.
How should I exclude/include certain fields from my query using Mongoose and paginate?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5
Top Results From Across the Web
How to Paginate with Mongoose in Node.js [Practical Examples]
It is a traditional way to paginate with mongoose. Here, It simply uses limit and offset in SQL queries to paginate the data...
Read more >How to paginate with Mongoose in Node.js? - Stack Overflow
Save this answer. Show activity on this post. Try using mongoose function for pagination. Limit is the number of records per page and...
Read more >mongoose-paginate-v2 - npm
Start using mongoose-paginate-v2 in your project by running `npm i ... {Object | String} - Fields to return (by default returns all fields)....
Read more >mongoose Pagination with NodeJS and MongoDB
This method has three parameters – the aggregate query, options, and a callback function. The second parameter will decide how pagination will ...
Read more >Server side Pagination in Node.js, MongoDB - BezKoder
This Mongoose Model represents tutorials collection in MongoDB database. These fields will be generated automatically for each Tutorial document ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am having the exact same issue with version 7.0.0
it seems this is caused by a bug;
mongoist API’s findAsCursor method uses projection as second param see docs
where as official mongodb driver uses options object as second param see docs
mongo-cursor-pagination
currently does not account for this difference see src code thus doesn’t properly pass the projection in the case mongodb native client is used