【发布时间】:2020-08-05 18:40:05
【问题描述】:
我需要通过他的mongodb集合的_id找到一个用户,
需要使用用户 ID 中的前 5 个字符进行搜索
5e9cca 的示例我可以找到集合 ID 5e9cca24beabb96a4caedc35
var id = req.body.id
User.findById({ _id : { $regex: '^id', $options: 'i' } },
(err, user) => {
if (err) {
console.log(err);
res.status(400)
}
使用这段代码我得到了这个错误:
MongooseError [CastError]: Cast to ObjectId failed for value "{ _id: { '$regex': '^id', '$options': 'i' } }" at path "_id" for model "User"
PS : 使用洞 id 的搜索正在工作
【问题讨论】:
标签: node.js regex mongodb mongoose mongodb-query