Creating, Listing, and Deleting Databases Example

To help solidify the database operations, Listing 13.3 illustrates the full process of creating, listing, and deleting databases. A connection is made to the MongoDB server, and then lines 4–7 list the current databases. Then lines 8 and 9 create a new database by calling create-Collection(). Inside the createCollection() callback handler, the databases are listed again to verify creation.

Lines 15–32 delete the database by using dropDatabase(). Notice that inside the drop-Database() callback, a setTimeout() timer is implemented to wait for a number of seconds before checking the list of databases to verify that the database was deleted. The output is shown in Figure 13.3.

Listing 13.3 db_create_list_delete.js ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.