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?
相关问题
- MongoDB can not create unique sparse index (duplic
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Spring Data MongoDB - lazy access to some fields
- Eager-loading association count with Arel (Rails 3
相关文章
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- Right way to deploy Rails + Puma + Postgres app to
- mongodb error: how do I make sure that your journa
- How to track MongoDB requests from a console appli
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
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:
For Mongoid 5:
Now we can perform queries on the collection
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
Now you can run Mongo Query using the driver (Moped or Mongodb-ruby driver) like this
This would give u the moped (if using mongoid 3) connection for that document
2) You can also do it something like this
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
The short answer is Moped. This is the lower-level API that Mongoid is built upon and will be available if you already use Mongoid. The Moped API is a thin wrapper around the raw MongoDB operations. The documentation here: http://mongoid.org/en/moped/docs/driver.html should be useful.
If you using mongoid 5(five) I would recommend using this.
The trick to this is the BSON::ObjectID. This is like in the mongo query if you want to search for a single id.
Above is the mongo version of the query. I found translating ruby code to mongo code is the hard part as there are a few pieces that can be a bit hard to find in the documentation.
http://www.rubydoc.info/gems/mongo/Mongo%2FCollection%3Aupdate_one
Like anyone has mention here , your answer is Moped. Here is my example for a ruby script (simple file test.rb)
development: sessions: default: database: test_development hosts: - localhost:27017 options:
2. Set load config and test collection