0

We're using the MongoHQ addon on Heroku, with the Mongoid 3.0 adapter. The addon plans come with a size limit, and Mongo will silently fail writing when the DB limit has been reached (unless configured for safe mode--in which case it'll throw exceptions).

I'm trying to query from within the app how close we are and send an alert if we've reached the limit. How can I run something like the db.stats() command but using Mongoid?

Wolfram Arnold
  • 7,159
  • 5
  • 44
  • 64

2 Answers2

1

I've found out how to do this in Mongoid 3.x which uses Moped as driver, not the Ruby driver from 10gen.

It was the author of Moped himself who answered a github issue raised on the matter.

Mongoid.default_session.command(collstats: 'collection_name')

This will return the same results as db.stats() from the Mongo console. As an additional bonus, if the collection is capped, there'll be a flag in the return values indicating that.

Wolfram Arnold
  • 7,159
  • 5
  • 44
  • 64
  • `Mongoid.default_session` is now deprecated, but the command above is still valid: just use `Mongoid.default_client.command(collstats: 'collection_name')` – Alessandro Jan 15 '16 at 10:41
0

You can call the ".db" method on your object (e.g. a Document), and do .stats on it.

For example:

MyBlog.db.stats

For verisons prior to Mongoid 3.0.0, Mongoid.master.stats should also work.

Manish Malik
  • 326
  • 2
  • 4
  • 1
    By object you mean a class that has `include Mongoid::Document`? One of my Mongoid classes is called `Metric`. Using your suggestion, I tried `Metric.db.stats`, and I get `undefined method 'db' for Metric:Class`. I also tried with an instance--same problem. Same with trying `Mongoid.default_session`. It seems there is no `db` method. I'm using Mongoid 3.0.10. – Wolfram Arnold Nov 20 '12 at 22:54