简体   繁体   English

将嵌入的文档 _id 保存为 ObjectId 但将其显示为字符串

[英]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.}我想将 Review 类中的 _id 保存为 ObjectId,但是当我查询它时,我希望它显示为字符串而不是 {"timestamp"、"machineIdentifier" 等}

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. mongodb 中的 Review 类是一个嵌入式文档。

When I query for a certain review with the _id as a String, it returns null.当我使用 _id 作为字符串查询某个评论时,它返回 null。 How can I display the ObjectId as a String during queries but let it remain as an ObjectId?如何在查询期间将 ObjectId 显示为 String 但让它保留为 ObjectId?

@JsonSerialize(使用 = ToStringSerializer.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌入文档保存,ID 为 null - embedded document save with id null Spring 数据 Mongodb 按嵌入文档 id 查询 - Spring Data Mongodb Query by embedded document id MongoDB / Morphia将技术ID保存为ObjectId,尽管它是Java中的String - MongoDB / Morphia saves technical id as ObjectId although it's a String in Java 在聚合中将 _id(ObjectId) 转换为 String 以进行查找 Spring boot - convert _id(ObjectId) to String in aggregation for lookup Spring boot 使用 java 在 Micronaut 中将 MongoDb ObjectId _id 更改为字符串 - Change MongoDb ObjectId _id to string in Micronaut using java mongo-java-driver如何使用字符串ID反射ObjectId - mongo-java-driver how to use string id reflection ObjectId Spring Mongo 存储库无法将字符串与 _id 字段中的 ObjectId 匹配 - Spring Mongo Repository is unable to match String to ObjectId in _id field 在属性中带有 ObjectId 的 MongoDB 文档 - MongoDB document with ObjectId in atributes 在MarkLogic中保存带有嵌入式二进制附件列表的JSON文档 - Save a JSON document with a list of embedded binary attachments in MarkLogic 如何在 android Firestore 的文档字段中保存文档 ID? - How to save document id in the document field in android Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM