how to get mongodb current featureCompatibilityVersion into a variable with only version

All we need is an easy explanation of the problem, so here it is.

how to get mongodb current featureCompatibilityVersion into a variable with only version

instead of printing like this

db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : { "version" : "4.0" }, "ok" : 1 }

Assign only version to a variable using mongo shell

fcv_version=$(eval "mongo <connection paramaeter> --eval '<oneliner to get fcv version>')
echo $fcv_version
4.0

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Use

fcv_version=$(eval "mongo <connection paramaeter> --quiet --eval 'db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ).featureCompatibilityVersion.version')

Method 2

Put that answer to variable and then read only wanted value:

cont:PRIMARY> var h=db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
cont:PRIMARY> h.featureCompatibilityVersion.version
4.4

At your requested form:

[mongodb_node ~]$ m_ver=$(mongo --quiet --eval 'db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 }).featureCompatibilityVersion.version')
[mongodb_node ~]$ echo $m_ver
4.4

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply