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

The question:

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

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

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


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 Comment