# login
$ mongosh --host mongodb.domain.com \
--port 27017 \
--username 'username' \
--password $(pass show path/to/credential) \
--authenticationDatabase 'db_name'
# or
$ mongosh 'mongodb://mongodb.domain.com:27017' \
--username 'username' \
--password $(pass show path/to/credential) \
--authenticationDatabase 'db_name'
Current Mongosh Log ID: 66b5a880d70a325006838725
Connecting to: mongodb://<credentials>@mongodb.domain.com:27017/?directConnection=true&authSource=db_name&appName=mongosh+2.2.15
Using MongoDB: 6.0.2
Using Mongosh: 2.2.15
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
test>
# use database
test> use db_name
switched to db db_name
db_name> db.hello()
{
isWritablePrimary: true,
topologyVersion: {
processId: ObjectId('6616e407a0520bbbc6dbeace'),
counter: Long('0')
},
maxBsonObjectSize: 16777216,
maxMessageSizeBytes: 48000000,
maxWriteBatchSize: 100000,
localTime: ISODate('2024-08-09T05:52:20.150Z'),
logicalSessionTimeoutMinutes: 30,
connectionId: 235951,
minWireVersion: 0,
maxWireVersion: 17,
readOnly: false,
ok: 1
}
utility
[!NOTE|label:references:]
db_name> help
db_name> db.collection.help()
# show dbs
test> show dbs
# get current db name
db_name> db.collection.getDB()
db_name
# get current collection name
db_name> db.collection.getName()
collection
# get stat of collection
db_name> db.collection.stats()
[!NOTE|label:references:]
db_name> db = db.getSiblingDB('db_name')
show all tables
[!NOTE|label:references:]
db_name> db.getCollectionNames()
# or
db_name> show tables
# or
db_name> show collections