BigInteger 在 spring mongo 中保存为 String

BigInteger is saved as String in spring mongo

public class Product {
    @Id
    private String productId;
    private BigInteger stock;
}

template.save(product);

库存在 mongo 集合中保存为字符串,例如:{"stock":"20"},它应该保存为 {"stock":20},如何将其保存为数字?

看看 source code,这就是 Spring 决定用作表示的内容。 潜在的问题是没有有效的数字 MongoDB datatype 可以用来可靠地表示 BigInteger 的所有可能值。