How to directly access the MongoDB API from Mongoid?

I would like to use the addToSet MongoDB method, but Mongoid does not currently support this. Is there a way to directly access the MongoDB driver from my Rails model?

+7
source share
3 answers

I asked this question in the Mongoid group and this was the best answer:

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

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

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

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

Alternatively, you can access the collection of records using ModelName.collection.

+8
source

You can use the mongodb mongo ruby ​​gem:

https://github.com/mongodb/mongo-ruby-driver

Check the update method in api:

http://api.mongodb.org/ruby/1.2.0/Mongo/Collection.html#update-instance_method

And this option may be what you are looking to use addToSet:

(Boolean) :upsert — default: +false+ — if true, performs an upsert (update or insert) 
0
source

Before and including Mongoid 2.4, you can access the database object using

 db = Mongoid.master 
0
source

All Articles