
- 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
Selecting database inside the JS in MongoDB?
You can use getSiblingDB() from MongoDB for this using var keyword from JS −
anyVariableName= db.getSiblingDB(‘yourDatabaseName’);
Let us implement the above syntax in order to select database −
> selectedDatabase = db.getSiblingDB('sample');
This will produce the following output −
Sample
Now, insert some documents. Let’s say the collection is ‘selectDatabaseDemo’ −
> db.selectDatabaseDemo.insertOne({"ClientName":"John Smith","ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cda794b5667f1cce01d55ad") } > db.selectDatabaseDemo.insertOne({"ClientName":"Carol Taylor","ClientAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cda79565667f1cce01d55ae") }
Display all documents from a collection with the help of find() method −
db.selectDatabaseDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cda794b5667f1cce01d55ad"), "ClientName" : "John Smith", "ClientAge" : 23 } { "_id" : ObjectId("5cda79565667f1cce01d55ae"), "ClientName" : "Carol Taylor", "ClientAge" : 24 }
- Related Articles
- Selecting random entry from MySQL Database?
- Creating and Selecting a MySQL Database
- Selecting only a single field from MongoDB?
- How to increment inside the update() function in MongoDB?
- Find inside a hash MongoDB?
- Get database data size in MongoDB?
- Update elements inside an array in MongoDB?
- Get the storage size of a database in MongoDB?
- Create and display the newly created database in MongoDB?
- Check if MongoDB database exists?
- Checking a table inside a schema in SAP HANA database
- Different database objects inside Catalog folder in SAP HANA Studio
- Display collections in a particular MongoDB database?
- How to drop a database in MongoDB?
- MongoDB Increment value inside nested array?

Advertisements