Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot fetch data #19

Closed
yulidai opened this issue Aug 9, 2018 · 25 comments
Closed

Cannot fetch data #19

yulidai opened this issue Aug 9, 2018 · 25 comments

Comments

@yulidai
Copy link

yulidai commented Aug 9, 2018

Query:
db.test.aggregate([{$group : {_id : "$name”, num_tutorial : {$sum : 1}}}])

Error in Response:
{ "message": "Unexpected token $ in JSON at position 3" }

I'm a novice at these things, can you help me what wrong with it?

@JamesOsgood
Copy link
Owner

JamesOsgood commented Aug 9, 2018 via email

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Query:
db.test.aggregate()

Error in Response
{ "message": "Cannot read property 'length' of undefined" }

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Thanks for your reply. I changed query to:
db.test.aggregate({"$group" : {"_id" : "$name", "num_tutorial" : {"$sum" : 1}}})

Then there's no wrong messge. But still not info showed in "Table Panel", but has result when running in mongo-cli

@minh0722
Copy link

minh0722 commented Aug 9, 2018

you need to project the following fields in order for it to show on the panel:

db.test.aggregate([
    {"$group" : {
        "_id" : "$name",
        "num_tutorial" : {"$sum" : 1},
        "time" : {"$first" : "$your_time_column_name"}
    }},
    {"$project" : {
        "name" : "$_id",
        "value" : "$num_tutorial",
        "ts" : "$time",
        "_id" : 0
    }}
])

You can check the README for more info on the required fields that the api expects

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

@minh0722 Thank you for your patience

Here is my data in mongo with command in mongo:

db.myCollection.find()

{ "_id" : ObjectId("5b6c2f134202c48ce09a6a13"), "time" : 1533816595792, "value" : 1, "metric" : "first" }
{ "_id" : ObjectId("5b6c2f1b4202c48ce09a6a14"), "time" : 1533816603521, "value" : 3, "metric" : "second" }
{ "_id" : ObjectId("5b6c34a74202c48ce09a6a15"), "time" : 1533818023968, "value" : 5, "metric" : "second" }

Then the query in panel is:

db.myCollection.aggregate([
    {"$group" : {
        "_id" : "$name",
        "num_tutorial" : {"$sum" : 1},
        "time" : {"$first" : "$time"}
    }},
    {"$project" : {
        "name" : "$_id",
        "value" : "$num_tutorial",
        "ts" : "$time",
        "_id" : 0
    }}
])

But still nothing showed in either Graph or Table. Any help will be appreciated, thank you!

@minh0722
Copy link

minh0722 commented Aug 9, 2018

You don't have a column name anywhere in your db. Try changing "_id" : "$name" to "_id" : "$metric"

@minh0722
Copy link

minh0722 commented Aug 9, 2018

Sorry I edited my prev comment

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

@minh0722 Thank you, Still nothing after changing, any other problem?

@minh0722
Copy link

minh0722 commented Aug 9, 2018

what's the response from the query?

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

target is

target:"db.myCollection.aggregate([ {"$group" : { "_id" : "$metric", "num_tutorial" : {"$sum" : 1}, "time" : {"$first" : "$time"} }}, {"$project" : { "name" : "$_id", "value" : "$num_tutorial", "ts" : "$time", "_id" : 0 }} ])"

response is

response:Array[1]
    0:Object
        columns:Array[0]
        rows:Array[0]
    type:"table"

@minh0722
Copy link

minh0722 commented Aug 9, 2018

change the type to timeserie. Table type is not supported

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Result after changing type to timeserie

response:Array[0]

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Maybe the reason is caused by time zone(here is china), but when i change time range from "last 6h" to "last 24h". But still nothing to show, i don't know why... T.T

@minh0722
Copy link

minh0722 commented Aug 9, 2018

I dont think that's the problem cause you dont have a "$match" pipeline in your aggregate. But just to try it out add this before "$group":
{ "$match" : { "time" : { "$gte" : "$from", "$lte" : "$to" } } },

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

No data too.

when run in mongo-cli with:

db.myCollection.aggregate([ {
  "$group" : { 
    "_id" : "$metric",
    "num_tutorial" : {"$sum" : 1},
    "time" : {"$first" : "$time"} }
}, {
  "$project" : { 
    "name" : "$_id", 
    "value" : "$num_tutorial", 
    "ts" : "$time", "_id" : 0 }
} ])

Will get data below:

{ "name" : "second", "value" : 2, "ts" : 1533816603521 }
{ "name" : "first", "value" : 1, "ts" : 1533816595792 }

But with nothing in grafana. Maybe caused by my grafana setting? But datasource test is success. And i rechecked config is same to the document

@minh0722
Copy link

minh0722 commented Aug 9, 2018

did you select the datasource that is above the query box?

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Here is the db config:

db:Object
  url:"mongodb://rpiread:rpiread@rpi-sensor-data-shard-00-00-ifxxs.mongodb.net:27017,rpi-sensor-data-shard-00-01-ifxxs.mongodb.net:27017,rpi-sensor-data-shard-00-02-ifxxs.mongodb.net:27017/test?ssl=true&replicaSet=rpi-sensor-data-shard-0&authSource=admin"
  db:"test"

And the mongodb is running in local, Datasource testing info is:

Data source is working

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Yes i did select the mongodb as the datasouce

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

Oh my god, the Datasource testing always success even though closing the database. I think is the proxy hide the error, i'll check it...

@minh0722
Copy link

minh0722 commented Aug 9, 2018

check if data source is correctly configured. I had problem with some proxy settings and I had to write the full proxy domain name in mongodb url so you can check that too

@yulidai
Copy link
Author

yulidai commented Aug 9, 2018

@minh0722 When i change "MongoDB URL" to localhost, the response is different:
target:

db.myCollection.aggregate([ {
  "$group" : { 
    "_id" : "$metric",
    "num_tutorial" : {"$sum" : 1},
    "time" : {"$first" : "$time"} }
}, {
  "$project" : { 
    "name" : "$_id", 
    "value" : "$num_tutorial", 
    "ts" : "$time", "_id" : 0 }
} ])

response:

response:Object
  message:"doc.ts.getTime is not a function"

@minh0722
Copy link

I think the field time has to be of type Date

@yulidai
Copy link
Author

yulidai commented Aug 10, 2018

Right, it's ok after changing the type of time, thanks!

@yulidai yulidai closed this as completed Aug 10, 2018
@jonymaldo
Copy link

jonymaldo commented Jun 1, 2020

Right, it's ok after changing the type of time, thanks!

@yulidai How was this problem solved? I have the same problem, I changed the time type to isodate but it doesn't work.

@Adhira-Deogade
Copy link

I am facing same issues - my query is this -
db.test_profile.aggregate ( [ { "$match" : { "job_name" : "metric_name", "start_time" : { "$gte" : "15" } } } ])

and response is -
response:Array[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants