问题描述
我正在通过教程学习Spring Data MongoDB( https://spring.io/指南/教程/数据/2/).在本教程中,成本类型为BigDecimal,如下所示,并将其作为字符串"存储在MongoDB中.因此,当我尝试通过cost字段获得升序排序的结果时,我得到了错误的结果.
I'm studying Spring Data MongoDB with a tutorial (https://spring.io/guides/tutorials/data/2/).In this tutorial, the type of cost is BigDecimal as following and it is stored as a "string" in MongoDB.So, when I tried to get a sort result with an ascending sort by the cost field, I got a wrong result.
我发现使用BigDecimal类是进行计算的最佳方法.但是,如果我在MongoDB中使用Spring Data MongoDB将数字保存为BigDecimal类型,它将被保存为字符串类型这样我会得到像本教程这样的错误排序结果.
I found that using the BigDecimal class is the best way for calculations.But, if I save numbers as a BigDecimal type with Spring Data MongoDB in MongoDB,it will be saved as a string typeand I will get a wrong sort result like this tutorial.
为了精确起见,我该怎么做才能得到正确的数字排序结果?您能帮我解决这个问题吗?提前非常感谢您.
What can I do to get a right sort result in terms of numbers for the sake of precision?Could you please help me to solve this problem?Thank you very much in advance.
例如,
1> MenuItem类
1> MenuItem class
@Document(collection = "menu")
public class MenuItem {
@Id
private String id;
@Field("itemName")
@Indexed
private String name;
private BigDecimal cost;
2>创建MenuItem的实例
2> create an instance of 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>排序结果
{"_id":ObjectId("53e982f0300475a4fbab8c32"),"_class":"com.yummynoodlebar.persistence.domain.MenuItem","itemName":"Yummy Rice","description":"Rice,Egg Fried" ,成分":[{名称":鸡蛋",描述":鸡肉鸡蛋"},{名称":米饭",描述":纯白米饭"}},成本": "211.99","minutesToPrepare":0}
{ "_id" : ObjectId("53e982f0300475a4fbab8c32"), "_class" : "com.yummynoodlebar.persistence.domain.MenuItem", "itemName" : "Yummy Rice", "description" : "Rice, Egg Fried", "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":"Yummy Rice","description":"Rice,Egg Fried" ,成分":[{名称":鸡蛋",描述":鸡肉鸡蛋"},{名称":米饭",描述":纯白米饭"}},成本": "211.99","minutesToPrepare":0}
{ "_id" : ObjectId("53e982f0300475a4fbab8c33"), "_class" : "com.yummynoodlebar.persistence.domain.MenuItem", "itemName" : "Yummy Rice", "description" : "Rice, Egg Fried", "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":花生米粉,非常适合",成分":[{名称":花生",描述":坚果"},{名称":鸡蛋",描述":在面条中使用"}, {"name":"Noodles","description":"Crisp,可爱的面条"}],"cost":"52.99","minutesToPrepare":0}
{ "_id" : ObjectId("53e982f0300475a4fbab8c2f"), "_class" : "com.yummynoodlebar.persistence.domain.MenuItem", "itemName" : "Yummy Noodles", "description" : "Peanutty Noodles, perfect for every occasion.", "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":花生米粉,非常适合",成分":[{名称":花生",描述":坚果"},{名称":鸡蛋",描述":在面条中使用"}, {"name":"Noodles","description":"Crisp,可爱的面条"}],"cost":"52.99","minutesToPrepare":0}
{ "_id" : ObjectId("53e982f0300475a4fbab8c30"), "_class" : "com.yummynoodlebar.persistence.domain.MenuItem", "itemName" : "Yummy Noodles", "description" : "Peanutty Noodles, perfect for every occasion.", "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":花生米粉,非常适合",成分":[{名称":鸡蛋",描述":在面条中使用"},{名称":花生",描述":坚果"}, {"name":"Noodles","description":"Crisp,可爱的面条"}],"cost":"52.99","minutesToPrepare":0}
{ "_id" : ObjectId("53e982f0300475a4fbab8c31"), "_class" : "com.yummynoodlebar.persistence.domain.MenuItem", "itemName" : "Yummy Noodles", "description" : "Peanutty Noodles, perfect for every occasion.", "ingredients" : [ { "name" : "Egg", "description" : "Used in the noodles" }, { "name" : "Peanuts", "description" : "A Nut" }, { "name" : "Noodles", "description" : "Crisp, lovely noodles" } ], "cost" : "52.99", "minutesToPrepare" : 0 }
推荐答案
不幸的是,BigDecimal
是 MongoDB本机不支持.这就是为什么我们默认将其转换为String
的原因.
Unfortunately, BigDecimal
is not supported by MongoDB natively. That's why we convert it into String
s by default.
对于更多的数值处理,尤其是当您处理价格之类的东西时,我们通常建议改为存储双精度数据,然后将其转换为BigDecimal
以便直接在域对象中进行算术运算.
For a more numeric handling, especially if you deal with prices or the like, we usually recommend to rather store doubles instead and convert them into BigDecimal
s for arithmetic operations in the domain object directly.
这篇关于在MongoDB中使用Spring Data MongoDB对存储为BigDecimal类型的数字值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!