-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
_id getter for ObjectIds #6415
Comments
👏 |
@djanowski I'd love to hear more about why you're so excited for this change if you care to share 👍 |
When dealing with populated or non-populated paths, when you need just the ID, we have a bunch of |
This breaks a lot of code for me. I have serializers that need to determine if a field is populated. I used to do that with I used to fall back to |
@freezy sorry about the inconvenience. Can you explain what you mean by "some queries are aggregations"? You can't use |
Thanks for having a look at this! My use case: I have a REST API that returns data from Mongoose. For every model I have a serializer that takes in a query result and spits out JSON objects, depending on the verbosity of the API call. The serializer doesn't know where the data comes from. It just checks which of the fields are populated and creates an object with the desired fields if they are available. The function doing the check looks like this: if (doc.populated && doc.populated(field)) {
return true;
}
let obj = get(doc, field);
if (isArray(obj) && obj.length > 0) {
obj = obj[0];
}
return obj && obj._id; Now, sometimes, the business layer uses aggregations to produce a result. The structure of what's passed to the serializer is identical, only without the Mongoose magic because of the aggregation. So in my serializer, I need a way of knowing if a field is populated for both a Mongoose document and a "plain" aggregation result. In case of an unpopulated field, the above code now returns true, because |
@freezy how about instead of |
Yeah, I also thought about if (doc.populated) {
return doc.populated(field);
} in the first block. Will do some tests and report back. Cheers! |
Thanks @freezy , let me know if that works for you 👍 |
Sorry it took so long, but while my workaround didn't work, yours did! Cheers and feel free to close. |
Hi @vkarpov15, we just found out about this issue and it seems like it does not list in the breaking changes from mongoose 4 -> 5 This is important since I am pretty sure lot of people do a check I agree that the check should be using either Thank you |
@ducdigital done. That's a good suggestion, thanks. |
I still believe it would be useful to have a universal approach for retrieving get an id regardless of whether a path is populated or not. Whenever I want to return the id of a reference in a document I need to do And whenever the reference is an array is the same thing but with a map It would also be nice if mongoose automatically implemented a virtual getter to retrieve the id or ids of the relations as strings instead of getting the documents or ObjectIds. |
Re: #6115, make it much easier to get an id regardless of whether a path is populated or not
The text was updated successfully, but these errors were encountered: