How can I see raw mongoDB queries with Mongoid

Teruki Shinohara picture Teruki Shinohara · Jul 31, 2013 · Viewed 8.4k times · Source

I followed this page to see mongoDB queries. As a result I could see Moped log.
But I can't see raw mongoDB queries.
How can I display MongoDB queries in the rails console/server

I did like the below.

# in [rails root]/config/environments/development.rb    
Mongoid.logger.level = Logger::DEBUG
Moped.logger.level = Logger::DEBUG
Mongoid.logger = Logger.new("#{Rails.root}/log/mongoid_development.log")
Moped.logger = Logger.new("#{Rails.root}/log/moped_development.log")

# in [rails root]/log/mongoid_development.log
# show nothing.

# in [rails root]/log/moped_development.log
MOPED: [ip address]:27017 QUERY        database=[database name] collection=[collection name] selector={"$query"=>{"screen_name"=>"ts_3156"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (54.6286ms)

How can I see raw mongoDB queries with Mongoid?
I want to see like the below.

db.[collection name].find({ $query: {"screen_name"=>"ts_3156"}, $orderby: {:_id=>1} })

I can see raw mongoDB queries in /var/log/mongo/mongo.log.
But I want to see raw queries in ORM(Mongoid)'s log.

Answer

eliotRosewater picture eliotRosewater · Jul 5, 2017

I think I got an answer. This is follow dsims answer and also from what I've seen in the documentation regarding logging.

I have an initialize file (config/initializers/mongoid.rb) and in there I have:

Mongoid.logger = Logger.new($stdout)
Mongo::Logger.logger = Logger.new($stdout)

It dumps out the mongo info the console. You probably want to change this for a production environment. But while developing I like to be able to see what the DB is doing. Especially because I'm brand new to MongoDb.