41

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.

Community
  • 1
  • 1
Teruki Shinohara
  • 783
  • 1
  • 9
  • 12
  • 2
    2 years later and still no decent answer? – Donato Feb 10 '15 at 19:03
  • Up! this is very helpful in order to familiarize CRUD in mongodb. – Charlie Mar 24 '15 at 18:30
  • For those looking to log moped to the console: `Moped.logger = Logger.new($stdout)` – dsims Aug 06 '15 at 17:39
  • 1
    check [this](http://stackoverflow.com/questions/27883178/get-raw-mongo-db-query-expression-that-mongoid-generate-it) out. Hope that it helps. – Hamdan Aug 11 '16 at 18:08
  • For future visitors, if you want beautiful colored logging similar to active record, you might want to look at [the mongo beautiful logger gem](https://github.com/redline-gh/mongo_beautiful_logger/) – Ibraheem Ahmed Jul 12 '20 at 23:41

3 Answers3

11

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.

eliotRosewater
  • 178
  • 1
  • 9
  • 2
    I can see the Mongo queries in this way the same as when I do `Rails.logger.level = :debug` or `config.log_level = :debug` but I just see them truncated `"pipeline"=>[{"$match"=>{"survey_uuid"=>{"$in"=>["4542ec5e0-ff90-0136-3d1c-02a18404aeea", "34dd05b0-6e8...` – fguillen Sep 02 '19 at 17:05
  • Great! It worked here. – Victor Aug 24 '23 at 15:21
1

I'm afraid that's how moped the mongodb driver mongoid uses, reports it's logs

https://github.com/mongoid/moped/blob/a995d859d85144b47523829f54384bd8d8a870dc/lib/moped/protocol/query.rb#L172

To see the raw queries you would need to modify this as you're doing now just tail the mongodb logs.

MatthewFord
  • 2,918
  • 2
  • 21
  • 32
0

To run query commands with Mongoid, Run mongo from the project directory to write your queries. Examples like show collections, db.users.find if there exist a users table.

Oluwayetty
  • 414
  • 4
  • 7