Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MongoDB version from Ruby code using Mongoid or Mongo Ruby Driver?

I have a Rails app with MongoDB. I need to dynamically display current MongoDB version on the status webpage. It's important for my client to know whether it's 3.0, 3.2 or 3.4 version.

However, I can't find this value anywhere. I'm using pry to debug the connection details and I've already tried any relevant public method from Mongoid and Mongo class hierarchy without success.

The canonical way to do this with pure MongoDB is db.version(), but it seems there is no way to send this raw query with Mongoid.

like image 338
yeasayer Avatar asked Oct 23 '25 00:10

yeasayer


1 Answers

You should do the following call (Mongoid 5+):

Mongoid.default_client.command(buildInfo: 1).first[:version]

Or, in earlier versions:

Mongoid.default_session.command(buildinfo: 1)["version"]

(Answer updated according to @yeasayer comment)

like image 158
gualopezb Avatar answered Oct 25 '25 15:10

gualopezb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!