
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Check if MongoDB database exists?
There are two possibilities to check if MongoDB database exists.
Case 1: The first possibility is that the MongoDB database exists i.e. it returns particular index.
Case 2: The second possibility is that the MongoDB database does not exist i.e. it returns index -1.
NOTE: An index starts from 0 and ends with (N-1) like an array.
The syntax is as follows to check if MongoDB database exists.
db.getMongo().getDBNames().indexOf("yourDatabaseName");
Case 1: Let us implement the above syntax to check if MongoDB database exists. Following is the query
db.getMongo().getDBNames().indexOf("test");
This will produce the following output
6
Look at the above sample output, we are getting 6 that means the database “test” exists and it is present at index 6.
Let us check all the databases now. Following is the query
> show dbs;
This will produce the following output
admin 0.001GB config 0.000GB local 0.000GB sample 0.001GB sampleDemo 0.000GB studentSearch 0.000GB test 0.009GB
Look at the above sample output, the database “test” exists and at index 6.
Case 2: If MongoDB database does not exist
> db.getMongo().getDBNames().indexOf("education");
The following is the output displaying -1 since the database “education” does not exist
-1
- Related Articles
- How to check if a MySQL database exists?
- Check that Field Exists with MongoDB?
- Check if value exists for a field in a MongoDB document?
- How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
- Ansible Check if a File Exists
- Check if table exists in MySQL and display the warning if it exists?
- Return True if a document exists in MongoDB?
- Check if a File exists in C#
- Swift Array Check if an index exists
- How to find if element exists in document - MongoDB?
- Drop Collection if already exists in MongoDB using Python
- Check if a file exists in Java\n
- How can I check whether a field exists or not in MongoDB?
- How to check if a variable exists in JavaScript?
- How do I check if a Python variable exists?
