question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Fields parameter with Mongoose paginate

See original GitHub issue

Current 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
kontextbewusstcommented, Jul 25, 2018

I am having the exact same issue with version 7.0.0

1reaction
chrisdostertcommented, Nov 13, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found