Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Fields parameter with Mongoose paginate #37

Open
ptrk8 opened this issue Jul 9, 2018 · 6 comments
Open

How to use Fields parameter with Mongoose paginate #37

ptrk8 opened this issue Jul 9, 2018 · 6 comments

Comments

@ptrk8
Copy link

ptrk8 commented Jul 9, 2018

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?

@ptrk8 ptrk8 changed the title Fields parameter for Mongoose paginate doesn't work How to use Fields parameter with Mongoose paginate Jul 9, 2018
@kontextbewusst
Copy link

I am having the exact same issue with version 7.0.0

@uncledent
Copy link

uncledent commented Sep 8, 2018

We have the same problem, quick fix is to do

List.paginate({
	query: query,
	limit: paginationLimit,
	fields: {
              fields: {
		"bids.bid": 0
                }
	})

This way it is excluded, but is ugly as hell

@uncledent
Copy link

Also a bad idea above, the _id field will disappear

@chrisdostert
Copy link

chrisdostert commented 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

@antony
Copy link

antony commented Dec 8, 2018

I'm having the same issue. I'm using the workaround above but re-including the id:

List.paginate({
  fields: {
    _id: 1,
    fields: {
      other: 1,
      items: 1 
    }
  }
})

@AraosDev
Copy link

AraosDev commented Oct 26, 2023

I'm having the same issue. I'm using the workaround above but re-including the id:

List.paginate({
  fields: {
    _id: 1,
    fields: {
      other: 1,
      items: 1 
    }
  }
})

A slight modification of this worked for me.

List.paginate({
  fields: {
    _id: 1,
   projection: { other: 1, items: 1 }
  }
});

Mentioning only projection property will strip of the _id property. If you do want the _id property, above workaround worked good for me.

The fix for the above issue is raised in the PR #357. But because of this issue (#361), I think PR is not getting approved and merged. Hope this is fixed sooner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants