如何使用 Mongoid 查看原始 mongoDB 查询 [英] How can I see raw mongoDB queries with Mongoid

查看:62
本文介绍了如何使用 Mongoid 查看原始 mongoDB 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这个页面查看了 mongoDB 查询.结果我可以看到轻便摩托车的日志.
但我看不到原始 mongoDB 查询.
如何在 rails 控制台中显示 MongoDB 查询/服务器

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)

如何使用 Mongoid 查看原始 mongoDB 查询?
我想看看下面的内容.

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} })

我可以在/var/log/mongo/mongo.log 中看到原始的 mongoDB 查询.
但我想在 ORM(Mongoid) 的日志中看到原始查询.

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

推荐答案

我想我得到了答案.这是按照 dsims 的回答以及我在有关日志记录的文档中看到的内容.

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

我有一个初始化文件 (config/initializers/mongoid.rb),在那里我有:

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)

它将 mongo 信息转储到控制台.您可能希望为生产环境更改此设置.但是在开发过程中,我希望能够看到数据库在做什么.尤其是因为我是 MongoDb 的新手.

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.

这篇关于如何使用 Mongoid 查看原始 mongoDB 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆