如何在Java中将字符串转换为MongoDB对象

huangapple go评论94阅读模式
英文:

How to convert string to object in MongoDB with Java

问题

我必须获取现有的 ID 以更新用户信息(例如电子邮件/姓名/密码)。我使用 POJO(普通Java对象)来映射对象 ID。我可以看到一个类似的帖子,但针对的是 .Net。

我该如何在将其传递给构造函数?(抱歉,我对 MongoDB 很新手)

String idString = request.getParameter("id"); // 出现错误:类型不匹配,无法从字符串转换为 ObjectId

ObjectId _id = new ObjectId(idString);

User user = new User(_id, email, fullName, password);
英文:

I have to get the existing id to update user information (e.g. email/name/password). I using POJOs to map the object id. I can see a similar post, but in .Net

How can I do that to pass it to the constructor? (Sorry, I'm very new to MongoDB)

ObjectId _id = request.getParameter("id"); // "erro with Type mismatch: cannot convert from String to ObjectId"


User user = new User(_id, email, fullName, password);

答案1

得分: 1

可以展示一下这个POJO(类),可能是由于您在模型中用于id的类型引起的...我无法告诉您问题是什么,但可以查看一下这个链接,可能会对您有所帮助:https://jira.mongodb.org/browse/JAVA-3677

英文:

can you show the POJO(class) it might be caused by the type you have user in your model for the id...I'm not able to tell you what is the problem but have a look at this link it might help you <https://jira.mongodb.org/browse/JAVA-3677>

huangapple
  • 本文由 发表于 2020年10月15日 21:16:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64372411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定