elixir-mongo / mongodb

MongoDB driver for Elixir
Apache License 2.0
568 stars 157 forks source link

Query returns a string instead of array #331

Closed BrainBuzzer closed 4 years ago

BrainBuzzer commented 4 years ago

I've been trying to query some data from mongodb. Here is the data that I get back from this library:

%{
  "_id" => #BSON.ObjectId<5e457024b0da5d86db02616e>,
  "email" => "user@email.com",
  "stats" => %{
    "hourlyStats" => [6, 1, 1, 3, 9, 3, 2, 11, 11, 17, 29, 36, 37, 67, 55, 29,
     41, 12, 17, 10, 14, 11, 19, 9],
    "weeklyStats" => '0\'O8DOQ'
  }
}

Notice the weeklyStats part. But here is the data that I get from MongoDB Compass, which is actually expected:

{
    "email": "user@email.com",
    "stats": {
        "weeklyStats": [{
            "$numberInt": "48"
        }, {
            "$numberInt": "39"
        }, {
            "$numberInt": "79"
        }, {
            "$numberInt": "56"
        }, {
            "$numberInt": "68"
        }, {
            "$numberInt": "79"
        }, {
            "$numberInt": "81"
        }],
        "hourlyStats": [Array of numbers]
    }
}

I've tried to fetch the similar data using NodeJS and it returns proper array. Any help on this?

ankhers commented 4 years ago

This is nothing specific to this library. It is how Elixir (and Erlang) handle lists of integers. For more information, take a look at Elixir's getting started guide[1].

[1] - https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#charlists

BrainBuzzer commented 4 years ago

Ohh, didn't know about this. Thanks a lot for clarifying.