如何直接在Ruby中触发原始MongoDB查询 [英] How to fire raw MongoDB queries directly in Ruby
问题描述
有什么方法可以直接在Ruby中触发原始mongo查询,而不是将其转换为本地Ruby对象吗?
Is there any way that I can fire a raw mongo query directly in Ruby instead of converting them to the native Ruby objects?
我经历了Ruby Mongo教程,但是在任何地方都找不到这种方法.
I went through Ruby Mongo Tutorial, but I cannot find such a method anywhere.
如果是mysql,我会触发这样的查询.
If it were mysql, I would have fired a query something like this.
ActiveRecord::Base.connection.execute("Select * from foo")
我的mongo查询有点大,并且可以在MongoDB控制台中正确执行.我想要的是直接在Ruby代码中执行相同的代码.
My mongo query is a bit large and it is properly executing in the MongoDB console. What I want is to directly execute the same inside Ruby code.
推荐答案
这是(可能)更好的微型教程,介绍了如何直接进入MongoDB的胆量.这可能无法解决您的特定问题,但可以使您达到SELECT * FROM table
的MongoDB版本.
Here's a (possibly) better mini-tutorial on how to get directly into the guts of your MongoDB. This might not solve your specific problem but it should get you as far as the MongoDB version of SELECT * FROM table
.
首先,您需要一个 Mongo::Connection
目的.如果
您正在使用 MongoMapper ,则可以调用connection
您的任何MongoMapper模型上的class方法以获取连接
或直接向MongoMapper询问:
First of all, you'll want a Mongo::Connection
object. If
you're using MongoMapper then you can call the connection
class method on any of your MongoMapper models to get a connection
or ask MongoMapper for it directly:
connection = YourMongoModel.connection
connection = MongoMapper.connection
否则,我想您会使用 from_uri
构建器
您自己的连接.
Otherwise I guess you'd use the from_uri
constructor to build
your own connection.
然后您需要使用数据库,您可以执行此操作
使用数组访问符号, db
方法,或获取
来自MongoMapper的当前直发:
Then you need to get your hands on a database, you can do this
using the array access notation, the db
method, or get
the current one straight from MongoMapper:
db = connection['database_name'] # This does not support options.
db = connection.db('database_name') # This does support options.
db = MongoMapper.database # This should be configured like
# the rest of your app.
现在,您的实例中有一个漂亮的 Mongo::DB
实例手.
但是,您可能希望 Collection
做任何有趣的事情
您可以使用数组访问符号或
collection
方法:
Now you have a nice shiny Mongo::DB
instance in your hands.
But, you probably want a Collection
to do anything interesting
and you can get that using either array access notation or the
collection
method:
collection = db['collection_name']
collection = db.collection('collection_name')
现在您的行为有点像SQL表,因此
您可以 count
或有多少东西使用 find
:
Now you have something that behaves sort of like an SQL table so
you can count
how many things it has or query it using find
:
cursor = collection.find(:key => 'value')
cursor = collection.find({:key => 'value'}, :fields => ['just', 'these', 'fields'])
# etc.
现在您拥有了真正的目标:烤箱刚出炉 Mongo::Cursor
指向您感兴趣的数据.Mongo::Cursor
是
一个 Enumerable
,因此您可以访问所有常规的迭代
诸如 each
, first
, each_with_object
:
And now you have what you're really after: a hot out of the oven Mongo::Cursor
that points at the data you're interested in. Mongo::Cursor
is
an Enumerable
so you have access to all your usual iterating
friends such as each
, first
, map
, and one of my personal
favorites, each_with_object
:
a = cursor.each_with_object([]) { |x, a| a.push(mangle(x)) }
还有 command
和<Mongo::DB
上的a href ="http://api.mongodb.org/ruby/current/Mongo/DB.html#eval-instance_method" rel ="noreferrer"> eval
方法可能会做什么你想要的.
There are also command
and eval
methods on Mongo::DB
that might do what you want.
这篇关于如何直接在Ruby中触发原始MongoDB查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!