简体   繁体   English

使用 java 在 Micronaut 中将 MongoDb ObjectId _id 更改为字符串

[英]Change MongoDb ObjectId _id to string in Micronaut using java

I have below Pojo class for the mongo entity我有下面的 Pojo class 用于 mongo 实体

public class Product {
    @BsonProperty("_id")
    @BsonId
    private ObjectId id;
    private String name;
    private float price;
    private String description;
}

when I fetch the mongo result as per the below code当我按照下面的代码获取 mongo 结果时

var item = Flowable.fromPublisher(this.repository.getCollection("product", Product.class).find()).blockingIterable();

The id id mapped something like this id id映射了这样的东西

在此处输入图像描述

However, in the database the id is something like this但是,在数据库中,id 是这样的

在此处输入图像描述

How do I map this id in the correct way, I am using Micronaut framework with java 15我如何以正确的方式使用 map 这个 id,我正在使用 Micronaut 框架和 java 15

You can add simple getter:您可以添加简单的吸气剂:

@JsonGetter
public String getObjectId() {
  return id.toString();
}

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

相关问题 MongoDB / Morphia将技术ID保存为ObjectId,尽管它是Java中的String - MongoDB / Morphia saves technical id as ObjectId although it's a String in Java 在Java中将ObjectID(Mongodb)转换为String - Convert ObjectID (Mongodb) to String in java 使用 Spring-data mongodb 在 Java 中使用 ObjectId 作为字符串(手册参考) - Using ObjectId as String in Java (Manual reference) with spring-data mongodb 使用Jongo和Jackson 2,如何将MongoDB ObjectId(在POJO中的String _id下表示)反序列化为十六进制字符串表示形式? - Using Jongo and Jackson 2, how to deserialize a MongoDB ObjectId (represented under String _id in a POJO) to an hexadecimal String representation? 如何在Java中使用ObjectID更新MongoDB中的文档 - How to update a document in MongoDB using ObjectID in Java 如何使用Java驱动程序3.4+在mongodb中计算带有ObjectId字段的唯一_id? - How to count unique _id with ObjectId field in mongodb using Java driver 3.4+? 将 MongoDB ObjectId 序列化为字符串 - Serialize MongoDB ObjectId to string Java 和 MongoDB 中的 ObjectId 类型 - ObjectId type in Java and MongoDB Java中的MongoDB ObjectId的Base64字符串表示形式? - Base64 string representation of MongoDB ObjectId in Java? 如何在 java spark 中将字符串值写为 MongoDB ObjectId? - How do I write string value as MongoDB ObjectId in java spark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM