question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to run mongodb query

See original GitHub issue

When I try to run this mongodb on both local install or redash demo, a error raised “Error running query: string indices must be integers”

Below is the query, I think this is an error of python code since I can run other simple query just fine.

{
    "collection": "Email",
    "aggregate": [
        {
            "$group": {
                "_id": {
                    "$dateToString": {
                        "format": "%Y-%m-%d",
                        "date": "$sendTime"
                    }
                },
                "sent": {
                    "$sum": {
                        "$cond": {
                            "if": {
                                "$gte": [
                                    "$sent",
                                    1
                                ]
                            },
                            "then": 1,
                            "else": 0
                        }
                    }
                },
                "opened": {
                    "$sum": {
                        "$cond": {
                            "if": {
                                "$gte": [
                                    "$opened",
                                    1
                                ]
                            },
                            "then": 1,
                            "else": 0
                        }
                    }
                },
                "clicked": {
                    "$sum": {
                        "$cond": {
                            "if": {
                                "$gte": [
                                    "$clicked",
                                    1
                                ]
                            },
                            "then": 1,
                            "else": 0
                        }
                    }
                }
            }
        },
        {
            "$limit": 10
        }
    ]
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
eranscommented, Feb 1, 2016

The format we use is slightly different in order to be able to maintain the order of fields, similar to what we do in regular queries.

Therefore, the query should look something like this:

{
  "collection": "places",
  "aggregate": [
    {
      "$group": {
        "_id": {"place_id": "$place_id", "place_name": "$place_name"},
        "count": { "$sum": 1 }
      }
    },
    {
      "$project": {
        "_id": "$_id.place_id",
        "name": "$_id.place_name",
        "count": "$count"
      }
    },
    {
     "$sort" : [
                {
                    "name" : "count",
                    "direction" : -1
                }
            ]
    }
  ]
}

You can see more examples here: http://docs.redash.io/en/latest/usage/mongodb_querying.html

2reactions
mderazoncommented, Feb 1, 2016

Getting the same error

{
  "collection": "places",
  "aggregate": [
    {
      "$group": {
        "_id": {"place_id": "$place_id", "place_name": "$place_name"},
        "count": { "$sum": 1 }
      }
    },
    {
      "$project": {
        "_id": "$_id.place_id",
        "name": "$_id.place_name",
        "count": "$count"
      }
    },
    {
      "$sort": {
        "count": -1
      }
    }
  ]
}

Fails on on the $sort operation with the same error message. (If I remove $sort it works fine) Works fine in mongo shell

re:dash 0.9.1+b1377

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed to run query after 10 retries :: caused by - MongoDB
I was running multiple aggregate/$merge (in python script) over the same collection and suddenly I got an error OperationFailure: “version ...
Read more >
failed to execute mongo like query - Stack Overflow
This is my mongo query and I'm passing it dynamically but its returning all data, I'm passing in postman body parameters as keyup[processor] ......
Read more >
Connecting to MongoDB fails - Aptible
Cause. This error is usually caused by attempting to connect without SSL to a MongoDB server that requires it, which is the case...
Read more >
MongoDB crashing. Unable to prune database
The only problem is that the database needs to run in order to do this, so that wont work. Next up i attempted...
Read more >
MongoDB - Redash
Simple Query Example; Count Query Example; Aggregation; MongoDB Extended JSON ... Redash will use them to override whatever is set in the connection...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found