Skip to content

how necessary is it to use ObjectId? #11471

Answered by vkarpov15
jimmywarting asked this question in Q&A
Discussion options

You must be logged in to vote

As long as _id is an ObjectId in userModel's schema, you shouldn't have to do ObjectId(). The below should be fine:

await someModel.find({ _id: req.body.id });

By default, schemas have an _id of type ObjectId. So if there's no _id in userModel's schema, the above code should still work.

Mongoose casts query filters to match your schema, so it can do things like convert strings to numbers or strings to ObjectIds: https://mongoosejs.com/docs/tutorials/query_casting.html

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Uzlopak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants