amjad
amjad

Reputation: 2916

Why mongoTemplate is returning number type (int) column values are as doubles?

I am trying to get uniq account_numbers from the following document.

{
    "_id" : ObjectId("5825e49785a4caf2bfa64a2f"),
    "profit" : "",
    "account_number" : 10,
    "m_number" : "",
    "registration_number" : "",
    "page_number" : "",
    "entry_date" : ISODate("2016-04-01T07:35:35Z"),
    "narration" : "By cash",
    "voucher_number" : "",
    "debit_credit" : 6150,
    "account_code" : 2102,
    "created_at" : ISODate("2016-04-01T07:35:35Z"),
    "updated_at" : ISODate("2016-04-01T07:35:35Z"),
    "employee_id" : 0,
    "balance" : 0,
    "credit" : 0,
    "debit" : 0,
    "particulars" : "",
    "since_last" : 0,
    "transaction_type" : 0,
    "voucher_path" : "",
    "branch" : "",
    "auto_voucher_number" : "",
    "check_book_series" : ""
}

The account_number type is number and I want to get it as int using Spring Mongo Template.

 Query query = new Query();
    query.addCriteria(Criteria.where("account_code").is(accountCode).and("account_number").exists(true));
    return List accounts = mongoTemplate.getCollection("transaction").distinct("account_number", query.getQueryObject());

Above code is returning List accounts. See debug results

 accounts = {ArrayList@6192}  size = 2815
 0 = {Double@6217} "1626.0"
 1 = {Double@6218} "1670.0"
 2 = {Double@6219} "2936.0"
 3 = {Double@6220} "2295.0"
 4 = {Double@6221} "1010.0"
 5 = {Double@6222} "1471.0"
 6 = {Double@6223} "3333.0"
 7 = {Double@6224} "1469.0"
 8 = {Double@6225} "3445.0"
 9 = {Double@6226} "3193.0"
 10 = {Double@6227} "219.0"
 11 = {Double@6228} "2509.0"
 12 = {Double@6229} "3750.0"
 13 = {Double@6230} "3425.0"

The short question is - how to get int type list from the document and why double types are returned ?

Here is the POJO for the document, may be I need to define something in the field ?

@Document(collection = "transaction")
public class Transaction implements Serializable {

    private static final Long serialVersionUID = 1L;

    @Id
    //@GeneratedValue(strategy = GenerationType.IDENTITY)
    @Field(value = "id")
    private String id;

    @Field(value = "account_code")
    private Integer accountCode;

    @Field(value = "account_number")
    private Integer accountNumber;

Upvotes: 0

Views: 1533

Answers (1)

Ori Dar
Ori Dar

Reputation: 19000

By default MongoDB saves numeric values as doubles:

For db.coll.save({x: 1}) : x will be represented (and converted by your Java driver) as 1.0 double.

To insert a 32 bit integer, you must do:

db.coll.save({x: NumberInt(1)})

and for 64 integers, do:

db.coll.save({x: NumberLong(1)})

The underlying Java driver will also save 1 as double (I think) via DBObject or Document.

If you look carefully at your code:

mongoTemplate.getCollection("transaction")...

Here you are falling back to DBCollection which returns a raw list of the account_number which is , as said, represented as double by default. MongoTemplate mappings and conversions are no relevant here: these are used for entire documents (for example the find methods) which is not the case here.

In order to use int here, you will have to convert to List value yourself, or change the account_number storage values to NumberInt.

Upvotes: 2

Related Questions