If i have a mongo document id as a string how do I query for it as an _id?

后端 未结 3 1559
旧巷少年郎
旧巷少年郎 2020-12-17 16:24

If i have a mongo document id as a string how do I query for it as an _id?

Will it work correctly to do .find({_id:\'stringID\'}) or do I need to conver

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 16:52

    Mongoose auto casts the string _id to ObjectId when you use findByID, so you can query using the string _id.

    Also remember that any input coming into your API from req.body or req.params will all be type string so you will need to type cast if needed..., but as mentioned, mongoose does that for you so you can just leave _id in the string format it comes in as.

提交回复
热议问题