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

I want to auto convert ObjectId to string #6996

Closed
annibuliful opened this issue Sep 8, 2018 · 7 comments
Closed

I want to auto convert ObjectId to string #6996

annibuliful opened this issue Sep 8, 2018 · 7 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Comments

@annibuliful
Copy link

Do you want to request a feature or report a bug?

What is the current behavior?
I use with Apollo server 2.0. when I want _id, but in response data, ObjectId is not converted to String.
What is the expected behavior?
How to auto convert ObjectId to String?
Please mention your node.js, mongoose and MongoDB version.
"mongoose": "^5.2.13",
node: 8
MongoDB: 3.6

@Lanfei
Copy link

Lanfei commented Sep 10, 2018

GraphQL have used a new method to serialize ID, the new method doesn't support ID in object type anymore, but following codes will help on this:

const mongoose = require('mongoose');

const ObjectId = mongoose.Types.ObjectId;

ObjectId.prototype.valueOf = function () {
	return this.toString();
};

Hope mongoose will support this feature soon :)

@vkarpov15
Copy link
Collaborator

Interesting idea @Lanfei , will investigate that for a future release.

@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Sep 16, 2018
@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Sep 16, 2018
@Lanfei
Copy link

Lanfei commented Sep 17, 2018

@vkarpov15 Nice, thanks :)

@cannap
Copy link

cannap commented Oct 24, 2018

Hi any news about this?

@vkarpov15
Copy link
Collaborator

@cannap #6912 should help with this, you'll be able to do:

mongoose.Schema.ObjectId.get(v => v != null ? v.toString() : v);

Does that API make sense?

@johnlife
Copy link

@vkarpov15, definitely, great idea, waiting for 5.4 patiently.
Thanks for great middleware.

@vkarpov15
Copy link
Collaborator

5.4.0 is out, here's the workaround

@vkarpov15 vkarpov15 removed this from the 5.x Unprioritized milestone Jan 3, 2019
@Automattic Automattic locked as resolved and limited conversation to collaborators Jan 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

5 participants