Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to directly access MongoDB API from Mongoid?

I'd like to use the addToSet method of MongoDB, but Mongoid doesn't currently support this yet. Is there a way of directly accessing the MongoDB driver from my Rails model?

like image 657
picardo Avatar asked Jan 24 '11 11:01

picardo


1 Answers

I asked this question on Mongoid group and this was the best response:

Assuming your model object is a Mongoid::Document, simply call the "db" method on it to get a handle to the Mongo::DB object that Mongoid is using under the hood.

http://rdoc.info/github/mongoid/mongoid/master/Mongoid/Collections/Cl...

From there, you can use the MongoDB Ruby driver API directly.

http://api.mongodb.org/ruby/current/file.TUTORIAL.html#

Also, you can access the record collection using ModelName.collection.

like image 110
picardo Avatar answered Oct 10 '22 17:10

picardo