Mongoid or MongoMapper?
I have used MongoMapper for awhile but decided to migrate to MongoId. The reason is hidden issues plus arrogance towards users. I had to jump through hoops to make MongoMapper work with Cucumber (succeeded in the end) and to put a couple of patches even the project was simple, but it's not the point. When I tried to submit a bug fix (due to incompatibility with ActiveRecord), they seemingly got pissed off that I found a problem and I was pushed around. While I was testing, I also encountered a major bug with their query implementation, while their testing was tuned in a way that the tests pass. After my previous experience, didn't dare to submit it.
They have a significantly lower number of pull requests and bug/feature submissions than MongoId, i.e. community participation is much lower. Same experience as mine?
I don't know which one has more features right now, but I don't see much future in MongoMapper. I don't mind fixing issues and adding functionality myself, but I do mind situations when they wouldn't fix bugs.
rails 4 with mongoid or mongomapper ? which is perfect for own product
Mongoid has been officially incorporated under the MongoDB umbrella. It is a more mature product and it's actively developed, compared with MongoMapper.
I'd definitely not use MongoMapper for a new project.
Advice on migrating from MongoMapper to Mongoid?
Both of them are great MongoDB Libraries for Ruby. But if you want to switch, here are some notes:
Migrating MongoMapper ORM to Mongoid ORM - Notes
Configure the database connection.
Replace configuration yaml file(includes replica configuration).
Configure Mongoid specific options. e.g -
raise_not_found_error: false
. if you don't want an error every time a query returns nothing...Change all models definations -
include MongoMapper::Document
toinclude Mongoid::Document
Change the format for all fields definitions.
In mongoid, you should specipy the timestamp:
include Mongoid::Timestamps
Change validation. e.g:
:in => ARRAY
, will be:validates :name, presence: true, inclusion: { in: ARRAY }
Change indexes.
Change order_by format. e.g: MM:
Model.all(:order => 'name')
. Mongoid:Model.order_by('name ASC')
Error
is a keyword in Mongoid. So if you have a model namedError
, you should change it.Pagination format is different, using another gem.
The primary key index entry in MM is
id
. In Mongoid it's_id
, if you have other code relying on.id
in the object JSON, you can override as_json function in your Model to create the JSON structure you want.In MM,
Model.fields(:id, :name)
,limits the fields returned from the database to those supplied to the method. In Mongoid it'sModel.only(:name,:id)
Some queries changes:
Selecting objects by array: MM:
Model.where(:attr.in => [ ] )
andModel.where(:attr => [ ] )
. Mongoid is only:Model.where(:attr.in => [ ] )
Map option of MM is equivalent to the Mid's pluck.
Model.map(&:name)
--to--Model.pluck(:name)
Mongoid doesn't support find query for nil. e.g:
value = nil. Model.find(value)
will throw an error :"Calling Document .find with nil is invalid"
. So in mongoid we should do:Model.find(value || "")
.In MM:
Model.find_or_initialize_by_name("BOB")
. In MongoidModel.find_or_initialize_by(name: "BOB")
.MM can be used in those two options:
Model.where({:name => 'BOB'}).first
, and alsoModel.first({:name => 'BOB'})
. Mongoid has only first option.In MM, to update multiple objects:
Model.set({conditions},attr_to_update)
. In Mongoid:Model.where(conditions).update_all(attr_to_update)
.
mongoid and mongomapper with eventmachine
From a quick peek at the mongoid and mongo_mapper source code, you would be correct. My approach would be to just clone mongoid (or mongomapper) and swap out the blocking http connections with async connections. The majority of the framework details are not tied to the async http connection and should be re-usable. Good luck!
Collections not being read correctly after mongomapper is replaced by mongoid and mongo driver is updated
I was able to figure out the issue by using the rails console and connecting to my database using the ruby mongo driver. (https://docs.mongodb.com/ruby-driver/master/quick-start/)
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'dbname')
db = client.database
db.collections # returns a list of collection objects
db.collection_names # returns a list of collection names
Using db.collection_names in the rails console I was able to see that any collection with a module was saved like this:
module_name.collection_name
After my upgrade the only collections names with modules I could read were:
module_name_collection_name
With this information, I added the following code to the ruby models affected:
store_in collection: 'module_name.collection_name'
This fixed my issue.
The reason collections without modules could be read without using the code above is because the collection names were simply stored as:
collection_name
Adding 'store in' in that case would just be redundant.
MongoMapper to Mongoid: How to search inside an array?
I assume your tasks is a separate document. So you can just replace all
with where
, it will work
def assigned_tasks_completed
self.company.tasks.where(:assigned_contacts => self.id.to_s, :completed => true)
end
For your another question,
Both mongoid set & update attributes are internally uses mongodb $set. But the difference is mongoid set is only accepts single field update, update_attributes accepts multiple.
Related Topics
Ruby: How to Load a File into Interactive Ruby Console (Irb)
Sort Array Returned by Activerecord by Date (Or Any Other Column)
Rails Mapping Array of Hashes Onto Single Hash
What Should Be Removed from Public Source Control in Ruby on Rails
Reraise (Same Exception) After Catching an Exception in Ruby
Concatenating String with Number in Ruby
How to Run Rake Tasks Within My Rails Application
Rails 4.0.1 on Heroku, Can't Create Database
Retrieving Array of Ids in Mongoid
File.Open, Open and Io.Foreach in Ruby, What Is the Difference
How to Define a 'Before_Save' Callback in a Module
If String Is Empty Then Return Some Default Value
Using Htaccess Password Protection on Rails
Converting Utc Timestamp to Iso 8601 in Ruby
How to Convert an Array of Strings into a Comma-Separated String