Jirico
Jirico

Reputation: 1262

How to see MongoDB raw queries in real time?

I'm using Mongoid but the query log it offers is very abstracted, it's far away from ActiveRecord that does a great job showing the raw SQL query. I also checked mongodb log files and it shows queries like:

query stock_system.companies query: { $query: { _id: ObjectId('53398f796a756e0e98040000') }, $orderby: { _id: 1 } } ntoreturn:1 idhack:1 keyUpdates:0 locks(micros) r:40 reslen:2022 0ms

But it's still abstracted and it does not show the queries in nested documents.

I'm having a hard time debugging queries. I tried to start mongod with profile level 2 and slowms set to -1. But it does not work, any tips?

#####EDIT: Example of log in nested documents query:

I'm trying a search in products nested model, like this:

current_user.company.products.where({name: /\A#{params[:name]}/}).limit(5)

And that query is not logged at all. All it shows is:

Processing by AjaxController#products as JSON
  Parameters: {"name"=>"meu", "page"=>"1", "_"=>"1397053974337"}
  MOPED: 127.0.0.1:27017 QUERY        database=stock_system collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('532a31376a756e29c9000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.5320ms
  MOPED: 127.0.0.1:27017 QUERY        database=stock_system collection=companies selector={"$query"=>{"_id"=>BSON::ObjectId('532a31376a756e29c9020000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.8050ms
Completed 200 OK in 4ms (Views: 0.2ms)

Which is the query for the current user and the company, but not the products embedded in the company. I need to see the product raw query to find out what's the problem with that query.

Upvotes: 3

Views: 4150

Answers (2)

Jirico
Jirico

Reputation: 1262

The real problem is that Mongoid fires a database query for the company document but not for the products embedded in it.

If I do a query like:

current_user.company.products.where({name: /\A#{params[:name]}/})

Company as the root document will be queried from the database, but the products regex query will be executed from a direct memory searching instead of a database query as I was expecting. Hence, there is no log from the products search and all indexes in products will be useless in this case.

If you want to fire a database query to use the index in a embedded document field, use like that:

Company.where("products.name" =>  /\A#{params[:name]}/)

That is the way to get the specific products in a database query, until MongoDB supports virtual collections for embedded documents.

Upvotes: 0

John Petrone
John Petrone

Reputation: 27517

Make certain that you're setting the profiling level correctly. Should be db.setProfilingLevel(2) to log all activity. Response should look like: { "was" : 0, "slowms" : 100, "ok" : 1 }

Once that's complete you should be able to query the system.profile collection in the database for a log of all activity - db.system.profile.find()

http://docs.mongodb.org/manual/reference/method/db.setProfilingLevel/

Upvotes: 1

Related Questions