Reputation: 8078
In a SQL database, you might run a query like select 1
just to verify that you have a good connection to the database without needing to know anything about the contents of the database or even have permission to access anything in it. Is there a similar sort of query I can run in MongoDB, say against a database with no collections?
Upvotes: 4
Views: 3604
Reputation: 4753
The easiest way it's running ping command. And it's preferred way to verify connection with the database.
Reading stats or listing dbs is a good way to do it as well, but the result can be nondeterministic (and I am not sure how it would work with different permissions).
https://docs.mongodb.com/manual/reference/command/ping/
Upvotes: 0
Reputation: 43245
You can do it in many ways :
1.Run a basic info command about the database :
db.stats()
db.serverStatus()
2- Run a mongoDb equivalent of mysql's 'show databases' or 'show tables'
'show dbs'
or
'use myDb'
'show collections'
Upvotes: 3