0

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

2 Answers 2

1

Use

fcv_version=$(eval "mongo <connection paramaeter> --quiet --eval 'db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ).featureCompatibilityVersion.version')
2
  • Your answer will include "Shell version and connection string" and not only version... So this is not requested answer.
    – JJussi
    Apr 15 at 7:15
  • @JJussi, correct I added --quiet option Apr 15 at 10:53
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
2
  • thanks, can we get this in one line ? because I would like assign to variable when ran from mongo shell. something like - fcv_version=$(eval "mongo <connection paramaeter> --eval '<oneliner to get fcv version>')
    – unknown
    Apr 14 at 2:28
  • 1
    That other answer didn't remember to put --quiet and that will generate extra stuff to the answer... If you didn't notice!
    – JJussi
    Apr 15 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.