QueryDSL NumberPath<BigDecimal> 大于从 MongoDB 集合中给出不准确的结果

QueryDSL NumberPath<BigDecimal> Greater Than giving inaccurate results from MongoDB Collection

在尝试为搜索过滤应用 QueryDSL 实现时,我遇到了以下问题,至少可以说这很令人困惑。

考虑名为 Payments 的 MongoDB 集合中的以下属性:

对象 1 - amountDue (Object) - amount => 106.00 (String)

对象 2 - amountDue (Object) - amount => 58.80 (String)

这些值是由系统生成的,amountDue对象的实际数据类型是org.joda.BigMoney对象。

这些属性应用于绑定方法,该方法用于提供 QueryDSL 谓词,这样任何具有 amountDue.amount 属性 大于搜索查询中指定的支付对象的支付对象都是 return编辑。这种方法描述如下:

@Override
public Predicate bind(NumberPath<BigDecimal> bigDecimalNumberPath, Collection<? extends BigDecimal> value) {
        ArrayList<? extends BigDecimal> amounts = new ArrayList<>(value);

        return bigDecimalNumberPath.gt(amounts.get(0));
    }

以下描述了我正在测试的案例,以及相应的结果:

{URL}/payments/filter?page=0&amountDue.amount=10.00,在内部转换为 'amountDue.amount > 10.00' 谓词 return 两个对象 [正确]

{URL}/payments/filter?page=0&amountDue.amount=20.00,在内部转换为 'amountDue.amount > 20.00' 谓词 returns Only Object 2 [Incorrect]

{URL}/payments/filter?page=0&amountDue.amount=60.00,在内部转换为 'amountDue.amount > 60.00' 谓词 returns 没有对象 [不正确]

{URL}/payments/filter?page=0&amountDue.amount=100.00,在内部转换为 'amountDue.amount > 100.00' 谓词 returns Only Object 2 [Incorrect]

{URL}/payments/filter?page=0&amountDue.amount=150.00,在内部转换为 'amountDue.amount > 150.00' 谓词 returns Only Object 2 [Incorrect]

当对象 1 的 amount 值更改为小于 100 的值时,所有情况 return 都会得到正确的结果。

请问你的suggestions/recommendations是什么?

感谢您的宝贵时间!!

已应用以下内容来解决上面发布的问题:

首先创建一个 Decimal128(Bson 类型)到 Big Decimal class 转换器:

public class Decimal128ToBigDecimalConverter implements Converter<Decimal128, BigDecimal> {

    @Override
    public BigDecimal convert(Decimal128 source) {
        return source.bigDecimalValue();
    }
}

然后创建一个 Big Decimal 到 Decimal128(Bson 类型)class 转换器:

public class BigDecimalToDecimal128Converter implements Converter<BigDecimal, Decimal128> {

    @Override
    public Decimal128 convert(BigDecimal source) {
        return new Decimal128(source);
    }
}

最后,配置您的 MongoConfig 文件以使用转换器:

@Bean
public MongoTemplate mongoTemplate() throws Exception {

    MongoTemplate mongoTemplate = new MongoTemplate(mongo(), getDatabaseName());
    MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
    mongoMapping.setCustomConversions(customConversions()); 
    mongoMapping.afterPropertiesSet();
    return mongoTemplate;

}

public CustomConversions customConversions() {
    return new CustomConversions(Arrays.asList(new Decimal128ToBigDecimalConverter(), new BigDecimalToDecimal128Converter()));
}


/* (non-Javadoc)
 * @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#mongo()
 */
@Bean
@Override
public Mongo mongo() throws Exception
{
    return new MongoClient();
}

解决方案已按照此处列出的示例实施:http://ufasoli.blogspot.com.mt/2017/06/custom-converter-for-mongodb-and-spring.html