Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire raw MongoDB queries directly in Ruby

Tags:

ruby

mongodb

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.

like image 991
bragboy Avatar asked Jul 01 '11 12:07

bragboy


People also ask

How do I run a MongoDB query in Robo 3T?

Open Studio 3T and connect to your MongoDB database. Next, open the Import Wizard from the toolbar. Then, choose JSON as the import format. Click OK.

How do I select a collection in MongoDB?

In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.

What query language does MongoDB use?

MongoDB uses the MongoDB Query Language (MQL), designed for easy use by developers. The documentation compares MQL and SQL syntax for common database operations.


2 Answers

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
like image 99
Andrew Avatar answered Sep 23 '22 10:09

Andrew


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.

like image 43
mu is too short Avatar answered Sep 24 '22 10:09

mu is too short