【问题标题】:how do I rename fields when performing search/find operation using mongodb(spring boot)?使用 mongodb(spring boot) 执行搜索/查找操作时如何重命名字段?
【发布时间】:2019-11-07 15:13:16
【问题描述】:

以下是“项目”的实体类。我需要获取所有项目名称为“xyz”的文档的字段“标题”和“项目名称”。检索到的文档中的字段名称“标题”应重命名检索时作为“renamedTitle”。

@Document (collection = "projectCollection")
@Data
@NoArgsConstructor
@AllArgsConstructor
 public class Project {
@Id 
private String projectId; 
private String projectName;
private String title;

}

 if the document collection is:
    {"_id":"1","projectName":"alpha","title":"start"},
    {"_id":"2","projectName":"beta","title":"mid"},
    {"_id":"3","projectName":"xyz","title":"last"}
The output expected is:
{"projectName":"xyz","renamedTitle":"last"}

【问题讨论】:

    标签: mongodb spring-boot spring-data-mongodb


    【解决方案1】:

    试试这个,

    @Json(name = "title")
    private String renamedTitle;
    

    @Json(name = "renamedTitle")
    private String title;
    

    以上其中一项应该可以解决。请让我们知道哪一个给出了预期的输出。

    【讨论】:

    猜你喜欢
    • 2014-07-10
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2015-06-22
    • 2018-11-29
    • 2018-06-24
    相关资源
    最近更新 更多