13

Is there any way that I can fire a raw mongo query directly in Ruby instead of converting them to the native Ruby objects?

I went through Ruby Mongo Tutorial, but I cannot find such a method anywhere.

If it were mysql, I would have fired a query something like this.

ActiveRecord::Base.connection.execute("Select * from foo")

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
bragboy
  • 34,892
  • 30
  • 114
  • 171

3 Answers3

23

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.


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

Otherwise I guess you'd use the from_uri constructor to build your own connection.

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.

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

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.

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

There are also command and eval methods on Mongo::DB that might do what you want.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
4

In case you are using mongoid you will find the answer to your question here.

Community
  • 1
  • 1
Nicolai Reuschling
  • 2,558
  • 2
  • 20
  • 24
2

If you're using Mongoid 3, it provides easy access to its MongoDB driver: Moped. Here's an example of accessing some raw data without using Models to access the data:

db = Mongoid::Sessions.default

# inserting a new document
collection = db[:collection_name]
collection.insert(name: 'my new document')

# finding a document
doc = collection.find(name: 'my new document').first

# "select * from collection"
collection.find.each do |document|
  puts document.inspect
end
Andrew
  • 227,796
  • 193
  • 515
  • 708