haohanyang / mongodb-datasource

MongoDB datasource for Grafana
Apache License 2.0
13 stars 6 forks source link

MongoDB Plugin Returns Array Data as Object Format Instead of Standard JSON Array #22

Open jeanfliu opened 1 day ago

jeanfliu commented 1 day ago

Hello,

I'm encountering an issue with the MongoDB plugin. When I run an aggregation query in Grafana, array data is returned in an object format rather than as a standard JSON array. Here’s an example:

Steps to Reproduce:

  1. Insert sample data in MongoDB:
    db.demoColl.insertMany([
    { "name": "series_1", "datas": ["a", "b"] },
    { "name": "series_2", "datas": [1, 2, 3, 4] }
    ])
  2. Execute the following aggregation query in Grafana:
    db.demoColl.aggregate([{ "$project": { "_id": 0 } }])

Environment:

The plugin currently returns array data in an object format, with each array element indexed as a key-value pair. Numeric values are wrapped in $numberDouble, which complicates direct usage within Grafana.

If the data source could return array data in a standard JSON array format, it could improve consistency and make data handling easier within Grafana.

Thanks for your work on this! Looking forward to your thoughts!

haohanyang commented 1 day ago

Good catch. I just realized a better way to unmarshall raw values

var a json.RawMessage
bson.UnmarshalValue(bson.TypeArray, rawValue.Value, &a)

or

var a json.RawMessage
bson.UnmarshalValue(bson.TypeEmbeddedDocument, rawValue.Value, &a)

I will soon fix it