Excluding a Collection from MongoMapper Queries

Skip to first unread message


Dec 7, 2017, 6:05:48 PM12/7/17
to MongoMapper
Hi all,

I'm very new to Ruby/Rails and I'm looking for help with MongoMapper

We currently have MongoMapper in our ruby app, however, I'm trying to isolate CRUD operations on a couple of collections to go through API calls instead of directly through MongoMapper. But in the same time I don't want these model objects to stop inheriting MongoMapper so that all the modeling and non DB operations continue without trouble.

In my limited Ruby knowledge I've decided to create a module that has the methods that I'd like to override and add that as a late include in each of the collection model class that I want to exclude.
However, it doesn't seem to be coming to my breakpoints. I did some tracing and I've found that the .find(...) calls are taking a different route that the querying.rb.

Based on that I've concluded that my research to find all the query points in MongoMapper was not conclusive. Can you help me identify or enumerate all the classes/modules in MongoMapper that has methods that perform mongo CRUD operations?

Thanks for reading.

Jamie Orchard-Hays

Dec 8, 2017, 11:38:57 AM12/8/17
to mongo...@googlegroups.com
Best thing to do is open the source code for MongoMapper and have a look at the code. It’s quite well organized and there are some examples and lots of tests.

I don’t really understand what it is you are trying to do. If you just want to use the Mongo driver directly on a collection, you can do so quite easily. For example:

class Foo
  include MongoMapper::Document


Have a look at the Mongo driver docs to see what methods are available. Make sure you’re viewing the 1.12.x docs, or whichever 1.x driver version you are using. You won’t be on the 2.x Mongo Ruby driver.


You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jamie Orchard-Hays

Dec 8, 2017, 11:40:38 AM12/8/17
to mongo...@googlegroups.com
By the way, if you are using Bundler, to view the MongoMapper source code. Here it is with atom as the editor:

EDITOR=atom bundle open mongo_mapper

Jon Kern

Dec 8, 2017, 4:54:33 PM12/8/17
to mongo...@googlegroups.com
I gotta say, as a newbie, this sounds like you could get into some sticky coding here if you are not careful…

If you let us know what you are trying to do from a functional point of view, we might have some better solutions that will be “clean,” so to speak.
  • Maybe you just need to create a PORO and add a Data Management layer?
  • Maybe you are trying to inject cross-cutting concerns across those models?
What leads you to think you needed to ask this group this question?

What problem are you trying to solve?

Maybe we can help :-)

On Dec 7, 2017, at 6:05 PM, mela...@momentfeed.com wrote:


Dec 8, 2017, 7:07:34 PM12/8/17
to MongoMapper
Thank you all for replying. Perhaps I wasn't clear enough. Our ruby app is currently heavily dependent on MongoMapper and the ::Document type structure. So there are abundant calls to .find() .update() .update_attribute(s) etc. The goal is to eliminate the use of MongoMapper on a few collections but not all. So I understand that I can go in the model class for said collection and remove include MongoMapper::Document but then that will destroy the model class completely. I want to continue to use it but I want to take over the calls that do CRUD operations, in other words, all the calls that result in a query to MongoDB, for example save() create() etc. as well as reads.

So I'm trying to achieve that by adding an include below MongoMapper::Document that has both class methods and instance methods that would catch these calls and redirect them to use our API instead of going directly to Mongo.

My challenge, is that I don't know which calls to override because MongoMapper library doesn't seem to put all the CRUD operations in one file. I used to think that querying.rb was the file I need to override but keys.rb also has some methods that go out. So I'm asking the group if you already know which classes / methods has the calls that result in a MongoDB connection.

Hopefully this clears things up.


Jamie Orchard-Hays

Dec 9, 2017, 1:29:32 PM12/9/17
to mongo...@googlegroups.com
Why do you want to eliminate use of MongoMapper on some models?

If you still want to use MongoDB as the data store, just keep the models as they are and access the driver directly, per my previous email.

If you do *not* want to use MongoDB as the data store for these models, then you are making an order of magnitude more work for yourself with the approach you have outlined. 

If you explain in more detail what you’re trying to do, we can advise you further.



Jon Kern

Dec 11, 2017, 7:48:43 PM12/11/17
to mongo...@googlegroups.com

Maybe you want some models to not be backed by Mongo, but rather use a different Data Store?

<going out on a limb*>
I am pretty sure you do not want to override any of MongoMapper’s CRUD methods.
</going out on a limb>

Lilly Rowling

Dec 29, 2021, 12:03:20 PM12/29/21
to MongoMapper
I've found another good reference to the idiom "going out on a limb"

Jon Kern

Dec 30, 2021, 8:16:54 AM12/30/21
to mongo...@googlegroups.com
Great reference, Lilly!

When I worked and lived in St. Petersburg, Russia, for a few years, I bought a book of 500 Russian/English idioms for one of my friends. 

O.M.G. They were hysterical — from both languages!

Reply all
Reply to author
0 new messages