Mongodb

如何將mongodb目前featureCompatibilityVersion轉換為只有版本的變數

  • April 15, 2022

如何將mongodb目前featureCompatibilityVersion轉換為只有版本的變數

而不是像這樣列印

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

使用 mongo shell 僅將版本分配給變數

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

採用

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

將該答案放入變數,然後只讀想要的值:

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

在您要求的表格中:

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

引用自:https://dba.stackexchange.com/questions/310870