gpt4 book ai didi

mongodb - 在 MongoDB 中使用 Spring Data MongoDB 对存储为 BigDecimal 类型的数值进行排序

转载 作者:可可西里 更新时间:2023-11-01 10:04:10 36 4
gpt4 key购买 nike

我正在通过教程 ( https://spring.io/guides/tutorials/data/2/ ) 学习 Spring Data MongoDB。在本教程中,成本的类型是 BigDecimal,如下所示,它在 MongoDB 中存储为“字符串”。因此,当我尝试按成本字段进行升序排序时,我得到了错误的结果。

我发现使用 BigDecimal 类是计算的最佳方式。但是,如果我在 MongoDB 中使用 Spring Data MongoDB 将数字保存为 BigDecimal 类型,它将被保存为字符串类型我会像本教程一样得到错误的排序结果。

为了精确起见,我该怎么做才能根据数字获得正确的排序结果?你能帮我解决这个问题吗?非常感谢您。

例如,

1>菜单项类

@Document(collection = "menu")
public class MenuItem {
@Id
private String id;

@Field("itemName")
@Indexed
private String name;
private BigDecimal cost;

2>创建MenuItem的实例

MenuItem item = new MenuItem();
item.setDescription("Peanutty Noodles, perfect for every occasion.");
item.setName("Yummy Noodles");
item.setCost(new BigDecimal("52.99"));


MenuItem item = new MenuItem();
item.setDescription("Rice, Egg Fried");
item.setName("Yummy Rice");
item.setCost(new BigDecimal("211.99"));

3>排序结果

db.menu.find().sort({cost:1})

{ "_id": ObjectId("53e982f0300475a4fbab8c32"), "_class": "com.yummynoodlebar.persistence.domain.MenuItem", "itemName": "香喷喷的米饭", "description": "米饭,鸡蛋炒饭", "ingredients": [ { "name": "Egg", "description": "Chicken Eggs"}, { "name": "Rice", "description": "Straight White Rice"} ], "cost": "211.99", "minutesToPrepare": 0

{ "_id": ObjectId("53e982f0300475a4fbab8c33"), "_class": "com.yummynoodlebar.persistence.domain.MenuItem", "itemName": "香喷喷的米饭", "description": "米饭,鸡蛋炒饭", "ingredients": [ { "name": "Egg", "description": "Chicken Eggs"}, { "name": "Rice", "description": "Straight White Rice"} ], "cost": "211.99", "minutesToPrepare": 0

{ "_id": ObjectId("53e982f0300475a4fbab8c2f"), "_class": "com.yummynoodlebar.persistence.domain.MenuItem", "itemName": "Yummy Noodles", "description": "Peanutty Noodles, perfect for每一次。", "ingredients": [ { "name": "Peanuts", "description": "A Nut"}, { "name": "Egg", "description": "Used in the noodles"}, { "name": "Noodles", "description": "Crisp, lovely noodles"} ], "cost": "52.99", "minutesToPrepare": 0 }

{ "_id": ObjectId("53e982f0300475a4fbab8c30"), "_class": "com.yummynoodlebar.persistence.domain.MenuItem", "itemName": "Yummy Noodles", "description": "Peanutty Noodles, perfect for每一次。", "ingredients": [ { "name": "Peanuts", "description": "A Nut"}, { "name": "Egg", "description": "Used in the noodles"}, { "name": "Noodles", "description": "Crisp, lovely noodles"} ], "cost": "52.99", "minutesToPrepare": 0 }

{ "_id": ObjectId("53e982f0300475a4fbab8c31"), "_class": "com.yummynoodlebar.persistence.domain.MenuItem", "itemName": "Yummy Noodles", "description": "Peanutty Noodles, perfect for每一次。", "ingredients": [ { "name": "Egg", "description": "Used in the noodles"}, { "name": "Peanuts", "description": "A Nut"}, { "name": "Noodles", "description": "Crisp, lovely noodles"} ], "cost": "52.99", "minutesToPrepare": 0 }

最佳答案

3.4 版本开始,MongoDB 通过新的十进制数据类型 (decimal128) 添加了对“BigDecimal”的支持。但是,spring data 仍然默认将 Java BigDecimal 映射到 MongoDB string(我想这是为了向后兼容)。好消息是可以通过在 spring boot 应用程序中注入(inject) CustomConversions 来简单地覆盖默认映射,如下所示:

@Bean
CustomConversions customConverions() {
Converter<Decimal128, BigDecimal> decimal128ToBigDecimal = new Converter<Decimal128, BigDecimal>() {
@Override
public BigDecimal convert(Decimal128 s) {
return s==null ? null : s.bigDecimalValue();
}
};

Converter<BigDecimal, Decimal128> bigDecimalToDecimal128 = new Converter<BigDecimal, Decimal128>() {
@Override
public Decimal128 convert(BigDecimal s) {
return s==null ? null : new Decimal128(s);
}
};

return new CustomConversions(Arrays.asList(decimal128ToBigDecimal, bigDecimalToDecimal128));
}

请查看this post for more detailed information and even a complete example .

关于mongodb - 在 MongoDB 中使用 Spring Data MongoDB 对存储为 BigDecimal 类型的数值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255697/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com