Check if mongodb database exists?

前端 未结 9 1480
孤街浪徒
孤街浪徒 2020-12-14 21:58

Is there a possibility to check if a mongo database allready exists?

9条回答
  •  自闭症患者
    2020-12-14 22:38

    From the shell, if you want to explicitely check that a DB exists:

    db.getMongo().getDBNames().indexOf("mydb");
    

    Will return '-1' if "mydb" does not exist.

    To use this from the shell:

    if [ $(mongo localhost:27017 --eval 'db.getMongo().getDBNames().indexOf("mydb")' --quiet) -lt 0 ]; then
        echo "mydb does not exist"
    else
        echo "mydb exists"
    fi
    

提交回复
热议问题