如何为嵌套 MongoDb 对象提供 ObjectID (Spring Boot)?

发布于 2025-01-19 01:02:43 字数 2858 浏览 0 评论 0原文

我有一个称为位置的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);
    }

但是, 评论。它是null,看起来像这样:

那么,如何确保所有注释具有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:
Image

So how can I ensure that all Comments have an ObjectID?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2025-01-26 01:02:43

看起来您缺少将位置文档与注释文档链接起来并将其指定为嵌套架构的注释。这是一个链接,您可以通过示例了解更多信息: 映射

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

活雷疯 2025-01-26 01:02:43

有两种可能的解决方案,不同的文档同一文档

在这两个解决方案中,您都必须在位置类中使用注释@dbref,在属性注释

@Data
public class Location {
    ...
    @DBRef
    private ArrayList<Comments> comments;
    ...
}

解决方案之间的区别在于类注释中,如果将注释保存在其他文档中,请使用注释@document,但是,如果您将其保存在同一文档,使用注释@Embedded(我想,您正在尝试执行此操作)。

不同的文档:

@Document
@Data
public class Comments {
...
}

同一文件:

@Embedded
@Data
public class Comments {
...
}

There are two possible solutions, different documents or the same document.

In both solutions, you must use the annotation @DBRef in the Location class, at the property comments.

@Data
public class Location {
    ...
    @DBRef
    private ArrayList<Comments> comments;
    ...
}

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:

@Document
@Data
public class Comments {
...
}

Same document:

@Embedded
@Data
public class Comments {
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文