如何为嵌套 MongoDb 对象提供 ObjectID (Spring Boot)?
我有一个称为位置
的mongoDB模型,另一个称为注释
的模型。
注释
嵌套在位置
的内部。我希望所有注释
对象具有一个objectid,但是现在ObjectID为null
。
位置模型:
@Data
public class Location {
@Id
private String id;
private String name;
private String latitude;
private String longitude;
private ArrayList<Comments> comments;
private String tags;
public Location() {
}
public Location(String name, String latitude, String longitude, ArrayList<Comments> comments, String tags) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.comments = comments;
this.tags = tags;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public ArrayList<Comments> getComments() {
return comments;
}
public void setComments(ArrayList<Comments> comments) {
this.comments = comments;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
注释模型:
@Data
public class Comments {
@Id
private String id;
private String text;
public Comments() {
}
public Comments(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
当我在这里登顶终点...
public Location createLocation(Location locDetails) {
Location location = new Location();
location.setName(locDetails.getName());
location.setLatitude(locDetails.getLatitude());
location.setLongitude(locDetails.getLongitude());
location.setComments(locDetails.getComments());
location.setTags(locDetails.getTags());
return locationRepo.save(location);
}
那么,如何确保所有注释
具有objectid?
I have a MongoDb model called Location
and another model called Comments
.
Comments
is nested inside of Location
. I would like all Comments
objects to have an ObjectID, but right now the ObjectID is null
.
The Location model:
@Data
public class Location {
@Id
private String id;
private String name;
private String latitude;
private String longitude;
private ArrayList<Comments> comments;
private String tags;
public Location() {
}
public Location(String name, String latitude, String longitude, ArrayList<Comments> comments, String tags) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.comments = comments;
this.tags = tags;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public ArrayList<Comments> getComments() {
return comments;
}
public void setComments(ArrayList<Comments> comments) {
this.comments = comments;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
The Comments model:
@Data
public class Comments {
@Id
private String id;
private String text;
public Comments() {
}
public Comments(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
But when I hit my end point here...:
public Location createLocation(Location locDetails) {
Location location = new Location();
location.setName(locDetails.getName());
location.setLatitude(locDetails.getLatitude());
location.setLongitude(locDetails.getLongitude());
location.setComments(locDetails.getComments());
location.setTags(locDetails.getTags());
return locationRepo.save(location);
}
...then everything gets created successfully in my database, other than the ObjectID for the comments. It is null
and looks like this:
So how can I ensure that all Comments
have an ObjectID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您缺少将位置文档与注释文档链接起来并将其指定为嵌套架构的注释。这是一个链接,您可以通过示例了解更多信息: 映射
It looks like you are missing annotations that would link the Location document with the Comment document and specify it as a nested schema. Here is a link where you can look learn more about it with examples: Mapping
有两种可能的解决方案,不同的文档或同一文档。
在这两个解决方案中,您都必须在
位置
类中使用注释@dbref
,在属性注释
。解决方案之间的区别在于类
注释
中,如果将注释保存在其他文档中,请使用注释@document
,但是,如果您将其保存在同一文档,使用注释@Embedded
(我想,您正在尝试执行此操作)。不同的文档:
同一文件:
There are two possible solutions, different documents or the same document.
In both solutions, you must use the annotation
@DBRef
in theLocation
class, at the propertycomments
.And the difference between the solutions is in the class
Comments
, if you save the comments in a different document, use the annotation@Document
, but, if you save it in the same document, use the annotation@Embedded
(I think, you are trying to do this one).Different document:
Same document: