EzDevInfo.com

mongomapper

A Ruby Object Mapper for Mongo MongoMapper

mongo_mapper or mongoid with rails4

The ORM mongo_mapper and mongoid both support ruby on rails. However, after reading their documents I still cannot make a decision which one to use to develop a new application with rails4.

Any suggestion?


Source: (StackOverflow)

N+1 problem in mongoid

I'm using Mongoid to work with MongoDB in Rails.

What I'm looking for is something like active record include. Currently I failed to find such method in mongoid orm.

Anybody know how to solve this problem in mongoid or perhaps in mongomapper, which is known as another good alternative.


Source: (StackOverflow)

Advertisements

How to configure MongoMapper and ActiveRecord in same Ruby Rails Project

I've got an existing production Ruby/Rails app that I want to migrate to MongoDB over time, as time permits, because it's not an option to just rewrite it all at one time. I'd love to be able to just deprecate my old classes as I get to them. I plan to use MongoMapper. I can't find an example where anyone explains how to set up the database config files to allow connection to both data stores within one app.

FWIW, I'm using Rails 3. I appreciate the help.


Source: (StackOverflow)

How to modify a gem?

I have a gem to which I'd like to do few changes.

I have it defined in my Gemfile as follows:

gem 'mongo_mapper", :git => "git://github.com/jnunemaker/mongomapper", :branch => "rails3" 

I was not able to find it in /usr/local/ruby/lib/ruby/gems/1.9.1/gems.


Source: (StackOverflow)

Mongodb: What to know before using? [closed]

I'm starting a hobby (non-revenue) project using Ruby on Rails. I've done a fair amount of development in Rails using Postgresql, and I can make a pretty good imitation of normalized schema. However, Mongrodb looks shiny and new. What better for trying out something new than a hobby project?

Think back to when you started using Mongodb. What techniques did you learn later that made you say, "If only I knew that when I started!" What plug-ins did you discover that you would have used right from the start, if only you had known? What references would you like to have had bookmarked?


Source: (StackOverflow)

How to write a Devise extension (to use a custom datastore)

I'd like to write an extension for Devise that allows you to use parse_resource as the datastore (as opposed to ActiveRecord). parse_resource is a Ruby wrapper for Parse.com's REST api. It's interface is pretty much the same as ActiveRecord's and is ActiveModel complaint. Because of this, it seems possible that an extension for Devise may not require too much non-boilerplate.

However, I can't find any tutorials. All I have to rely on are the sources for other extensions. From the MongoMapper extension, I gather that there are two main parts:

  1. The generators (not quite required)

    Here you overwrite DeviseGenerator#(generate_model|inject_devise_content|replace_default_devise_orm) methods.

  2. The "guts" (very much required)

    I'm not quite as sure what's going on here. It seems that there's a lot of boilerplate, with a little bit of custom type-casting, and at the bottom there's a declaration that we'll be using this extension instead of the default ORM.

Is that all there is to it? What am I missing? Can someone explain what happens in "the guts" in a bit more detail?

Are there any simple lint tests to run to ensure full compatibility with Devise?


Source: (StackOverflow)

MongoMapper and migrations

I'm building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model:

class SomeModel
  include MongoMapper::Document
  key :some_key, String
end

Later in version 2, I realize that I need a new required key on the model. So, in version 2, SomeModel now looks like this:

class SomeModel
  include MongoMapper::Document
  key :some_key, String
  key :some_new_key, String, :required => true
end

How do I migrate all my existing data to include some_new_key? Assume that I know how to set a reasonable default value for all the existing documents. Taking this a step further, suppose that in version 3, I realize that I really don't need some_key at all. So, now the model looks like this

class SomeModel
  include MongoMapper::Document
  key :some_new_key, String, :required => true
end

But all the existing records in my database have values set for some_key, and it's just wasting space at this point. How do I reclaim that space?

With ActiveRecord, I would have just created migrations to add the initial values of some_new_key (in the version1 -> version2 migration) and to delete the values for some_key (in the version2 -> version3 migration).

What's the appropriate way to do this with MongoDB/MongoMapper? It seems to me that some method of tracking which migrations have been run is still necessary. Does such a thing exist?

EDITED: I think people are missing the point of my question. There are times where you want to be able to run a script on a database to change or restructure the data in it. I gave two examples above, one where a new required key was added and one where a key can be removed and space can be reclaimed. How do you manage running these scripts? ActiveRecord migrations give you an easy way to run these scripts and to determine what scripts have already been run and what scripts have not been run. I can obviously write a Mongo script that does any update on the database, but what I'm looking for is a framework like migrations that lets me track which upgrade scripts have already been run.


Source: (StackOverflow)

How to mix mongodb and a traditional db in Rails?

I am considering using MongoDB (mongo-mapper) for a portion of my rails application. I am not ready to go whole hog MongoDB because there are too many useful gems that depend on a traditional DB.

That being said there are parts of my application that would be great to leverage a document database.

Has anyone had success mixing the two approaches? How do you link activerecord models with mongomapper models?


Source: (StackOverflow)

What is your preferred ODM in Ruby? MongoMapper, MongoID or MongoDoc?

In Ruby, there are currently 3 ODM (object data mappers) maintained:

Which is your preferred and why?


Source: (StackOverflow)

Understanding MongoDB (and NoSQL in general) and how to make the best use of it

I am beginning to think that my next project I am wanting to do would work better with a NoSQL solution. The project would either involve a ton of 2-column tables or a ton of dynamic queries with dynamically generated columns in a traditional SQL database. So I feel a NoSQL database would be much cleaner.

I'm looking at MongoDB and it looks pretty promising. Anyway, I'm attempting to make sense of it all. Also, I will be using MongoMapper in Ruby.

Anyway though, I'm confused as to how to layout things in such a freeform database. I've read http://stackoverflow.com/questions/2170152/nosql-best-practices and the answer there says that normalization is usually bad in a NoSQL DB. So how would be the best way of laying out say a simple blog with users, posts, and comments?

My natural thought was to have three collections for each and then link them by a unique ID. But this apparently is wrong? So, what are some of the ways to lay out such a thing? My concern with the answer given in the other question is, what if the author's name changed? You'd have to go through updating a ton of posts and comments. But is this an okay thing to do with NoSQL?


Source: (StackOverflow)

ERROR Errno::ECONNRESET: Connection reset by peer

gem 'rails', '3.0.0'
gem 'devise'
gem 'bson_ext', '>= 1.0.7'
gem 'bson', '>= 1.0.7'
gem 'mongo_mapper', :branch => 'rails3', :git => 'http://github.com/jnunemaker/mongomapper.git'
gem 'devise-mongo_mapper', :git => 'git://github.com/collectiveidea/devise-mongo_mapper'

With the above setup I get the following errors on requests:

Started GET "/users/sign_out" for 127.0.0.1 at 2010-09-27 13:16:30 +0300
  Processing by Devise::SessionsController#destroy as HTML
Redirected to http://localhost:3000/
Completed 302 Found in 19ms
[2010-09-27 13:16:31] ERROR Errno::ECONNRESET: Connection reset by peer
    /usr/local/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
    /usr/local/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
    /usr/local/ruby/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'


Started GET "/users/edit" for 127.0.0.1 at 2010-09-27 13:16:35 +0300
  Processing by Devise::RegistrationsController#edit as HTML
Completed   in 16ms
[2010-09-27 13:16:35] ERROR Errno::ECONNRESET: Connection reset by peer
    /usr/local/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
    /usr/local/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
    /usr/local/ruby/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

The user model:

class User
  include MongoMapper::Document
  plugin MongoMapper::Devise
  devise :registerable, :database_authenticatable, :recoverable
end

Ideas?


Source: (StackOverflow)

Understanding MongoMappers many relationship

Ok, so firstly Im not very in the know when it comes to Ruby, Rails, Mongo or even ActiveRecord so I apologise if these questions are fairly basic. Firstly, I have an "Event" model and I have added a many relationship to my "Comment" model. Now it is my (basic) understanding of mongo (or any document db) that foreign key lookups (or whatever they are called in mongo) are not advised, instead opting for storing as much as possible in a single document. With this in mind I would expect to have a single Events collection with the Comment JSON/BSON embedded in each document i.e.

event:

{
 Title: "some event"
 Comments : [
    { user_id: [object id], comment: "comment"}, 
    { user_id: [object id], comment: "other comment"}
 ]
}

Instead I am seeing that a comments collection has been created as well as the event collection. Would this be the correct way to map this relationship, if so how do I stop the comment collection been created and instead have the info embedded directly into the event? My guess is that I have to tell my Comment mapping not to have a _id property and thus not belong to its own collection.


EDIT: I found out that what I was looking for here in my first question was to use MongoMapper::EmbeddedDocument instead of including MongoMapper::Document


Secondly, I want users to be able to flag themselves as "attending" or just "interested" an event, which I assumed would be modelled as an array of reference user_id's on the event. So I initially thought it would be mapped as two "many" relationships, but as far as I can tell the name of the constant I am passing to the many method is used to create the getter/setters, which wouldn't work for me if I had two many relationships for the same type.

In other words, if I have this mapping:

class Event
   many :users
end

Then it's my understanding that I will then have getters and setters generated for the users property. So I can do something like:

event.users << someAttendingUser

This is all good, and at this point in time I would want the reference to the user to be stored and not the whole user BSON (unlike with the comments example above). Now the problem is how do I do this when I have two many mappings to the user collection, as with my need for both "attending" and "interested" users?

So to summarise this rambling:

  • Sometimes I want to store many relationships directly as BSON in the orinal document, and not as a reference to a document in another collection. How do I do this?

  • Sometimes I want to store many relationships of the same type on a document and I DO want them to be references to a document in another collection. How do I do this?

Hope this made sense, and I apologise if Im asking the obvious here.


EDIT 2:

Ok, well I really did search before asking this question, but it appears that I have now found the answer to both of my problems so I will close the question myself.

The second part required me to specify a class name option for the mapping i.e.

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end


Source: (StackOverflow)

How do i delete an embedded document in mongomapper?

Hi Guys I run a sinatra application with mongomapper. I have models called Movie(Document) and Cover(EmbeddedDocument).I embed covers into movies using

@movie.covers << @cover
@movie.save

This works great. when hit @movies.covers I got the array of embedded documents. But I am not able to destroy the embedded document. I tried something like this

@movie.covers.each do |c|
c.destroy
end

NoMethodError: undefined method `destroy' for #<Cover:0xb7b20734>
 from (irb):5
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `call'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `method_missing'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `each'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `send'
 from /usr/lib/ruby/gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/associations/proxy.rb:85:in `method_missing'
 from (irb):4
 from :0

Can anyone temme how to destroy it? It would be great if someone enlightens me how to update the embedded document.


Source: (StackOverflow)

MongoMapper and bson_ext problem

I can't get MongoMapper to work with my Rails app. I get this error message:

**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance. You can install the extension as follows: gem install bson_ext

If you continue to receive this message after installing, make sure that the bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.

I have installed DevKit and installed the gem: gem install bson_ext --no-rdoc --no-ri (result: bson_ext-1.0.1 installed)

I'm running on Windows 7. The Rails version is 2.3.7. I used the RubyInstaller when installing. Can anyone point me in the right direction?


Source: (StackOverflow)

Rails + MongoMapper + EmbeddedDocument form help

I am working on a pretty simple web application (famous last words) and am working with Rails 2.3.5 + MongoMapper 0.7.2 and using embedded documents. I have two questions to ask:

First, are there any example applications out there using Rails + MongoMapper + EmbeddedDocument? Preferably on GitHub or some other similar site so that I can take a look at the source and see where I am supposed to head? If not ...

... what is the best way to approach this task? How would I go about creating a form to handle an embedded document.

What I am attempting to do is add addresses to users. I can toss up the two models in question if you would like.

Thanks for the help!


Source: (StackOverflow)