4

How do I get the exact query sent from gmongo driver to mongod (for debugging purposes).

In mysql it is achieved by adding logSql = true to datasource.

I am writing an aggregate query with a matching pipeline between a start and end date. The dates are given as normal java Date classes.

db.collection.aggregate(
            [
                $match:
                    [
                        dateIssued: [

                            $gte: fromDate,

                            $lte: toDate
                        ]
                    ]
            ], 
            [
                $group:
                    [
                        _id: "\$type", 
                        total: 
                            [
                                $sum: 1
                            ]
                    ]
            ])

And it doesn't seem to filter the results based on the date. It's giving me back everything.

dsharew
  • 10,377
  • 6
  • 49
  • 75
Merhawi Fissehaye
  • 2,482
  • 2
  • 25
  • 39

2 Answers2

0

There is no way to do this from the driver, I found the best way to do it is use mongosniff. See http://docs.mongodb.org/manual/reference/program/mongosniff/

Graeme Rocher
  • 7,985
  • 2
  • 26
  • 37
  • Graeme, not sure if this is the right place to notify you but we've been unable to solve http://stackoverflow.com/questions/30714307/grails-mongo-gorm-mysteriously-thinks-a-object-referenced-by-an-embedded-fiel. We've tried stepping through what mongodb plugin tries to to do to no avail. I hope you will take a few secs to look at my question. Thanks – Alexander Suraphel Jun 16 '15 at 11:47
0

How do I get the exact query sent from gmongo driver to mongod (for debugging purposes)?

Turn on mongodb profiling and you can log all your queries.

In mysql it is achieved by adding logSql = true to datasource.

I think you want this to automatically happen based on your configuration in Grails. Then, do the following:

In Config.groovy:

mongo.profiling.enabled = true 

In Bootstrap.groovy:

GMongo mongo = new GMongo(grailsApplication.config.grails.mongo.host, grailsApplication.config.grails.mongo.port)
DB db = mongo.getDB(grailsApplication.config.grails.mongo.databaseName)

if( grailsApplication.config.grails.mongo.username ) {
    boolean auth = db.authenticate(grailsApplication.config.grails.mongo.username ,
            grailsApplication.config.grails.mongo.password ? grailsApplication.config.grails.mongo.password.toCharArray() : "" )

    if( !auth ) {
        log.error( "MongoDB failed to authenticate")
        return
    }
}

if( grailsApplication.config.mongo.profiling.enabled ) {
    db.setProfilingLevel(2)

}
else {
    db.setProfilingLevel(0)
}
Community
  • 1
  • 1
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90