Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query MongoDB directly from Ruby instead of using Mongoid?

I am writing a migration for a Rails application that uses MongoDB and Mongoid. My migration currently uses my models that use Mongoid to query and update records, but the performance is sub-par. I am essentially updating all records in a large collection and making n+20 queries. I killed the migration after taking an hour to run locally (and didn't finish). I would like to be able to run raw queries to mongo without too much effort. I'm assuming there is some way to access a mongo driver from Mongoid since Mongoid has already loaded a connection to the database. How can I access the database to run my update queries direcly?

like image 479
Andrew Avatar asked Feb 11 '13 16:02

Andrew


People also ask

Which method is used to query data from MongoDB collection?

The find() Method To query data from MongoDB collection, you need to use MongoDB's find() method.

Which query language is used by MongoDB?

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


4 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  # iterating over all documents in a collection collection.find.each do |document|   puts document.inspect end 
like image 169
Andrew Avatar answered Sep 22 '22 06:09

Andrew


For Mongoid 5:

db = Mongoid::Clients.default  collection = db[:collection_name] 

Now we can perform queries on the collection

like image 29
Venu Avatar answered Sep 25 '22 06:09

Venu


Here how you do it (This would work for 2+ and 3+ as well)

1) All your Model exhibit this behavior you have include Mongoid::Document inside all your model so technically each document is mapped in monogodb thru moped or mongodb-ruby driver via mongoid

so If you have model Like

class PerformerSource 
  include Mongoid::Document
  ## Definition

end

Now you can run Mongo Query using the driver (Moped or Mongodb-ruby driver) like this

PerformerSource.collection.insert("something")
## where something is json document you want to insert

This would give u the moped (if using mongoid 3) connection for that document

2) You can also do it something like this

 Mongoid::Sessions.default.collections.find { |document| document.name == "performer_sources"}.insert("something")

How to more on mongo query and how mongoid map those using moped u can follow this section of querying where it describe how query is acheived internally via moped

Hope this help

like image 27
Viren Avatar answered Sep 24 '22 06:09

Viren


For Mongoid 6:

db = Mongoid::default_client
collection = db[:collection_name]
like image 33
Josh Avatar answered Sep 21 '22 06:09

Josh