简体   繁体   中英

Save an embedded document _id as ObjectId but display it as a String

I want to save the _id in Review class as an ObjectId but when I query it I want it to be displayed as a String and not the {"timestamp", "machineIdentifier", etc.}

Here are the classes I made:

@Document
public class Catalog {
    ....
    private List<Review> reviews;
    ....
}

public class Review {
    @Id
    private ObjectId _id = new ObjectId();
    private String userid;
    private String username;
    private String reviewstring;
}

The class Review in the mongodb is an embedded document.

When I query for a certain review with the _id as a String, it returns null. How can I display the ObjectId as a String during queries but let it remain as an ObjectId?

@JsonSerialize(使用 = ToStringSerializer.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM