Question: In this exercise, you get a chance to create a simple MongoDB shell JavaScript file that creates a database and verify that the database is
In this exercise, you get a chance to create a simple MongoDB shell JavaScript file that creates a database and verify that the database is created. Use the following steps to create a new database named newDB:
1. Make sure the MongoDB server has been started.
2. Create a file named c:\code\lab10\db_create.js.
3. Add the code shown below to the file. This code connects to the MongoDB server on the local host and creates a new database named newDB by adding a new collection named newCollection to it.
| mongo = new Mongo("localhost"); newDB = mongo.getDB("newDB"); newDB.createCollection("newCollection"); |
4. Save the file.
5. Open a console prompt (i.e. windows command line prompt) and navigate to the c:\code\lab10\ location.
6. Start the MongoDB shell by typing mongo at the console prompt.
7. Execute the file created in Step 2 using the following shell command:
load("db_create.js")
8. Use the following command to verify that the new database newDB exists:
show dbs
9. Now, answer the following questions:
Write down the command to change the database to newDB and copy down the output. [10pts]
Write down the command to display the collection list within newDB and copy down the output. [10pts]
10. Save the answers from Step 9 in output10-1.txt.
Exercise 2 Delete a Database
In this exercise, you get a chance to create a simple MongoDB shell JavaScript file that deletes a database and verify that it has been deleted. Use the following steps to delete the new database named newDB you created in Exercise 1.
1. Make sure the MongoDB server has been started.
2. Create a file named c:\code\lab10\db_delete.js.
3. Add the code shown below to the file. This code connects to the MongoDB server on the local host and deletes the new database named newDB that was just created in Exercise 1.
| mongo = new Mongo("localhost"); myDB = mongo.getDB("newDB"); myDB.dropDatabase(); |
4. Save the file.
5. Open a console prompt and navigate to the c:\code\lab10\ location.
6. Start the MongoDB shell by typing mongo at the console prompt.
7. Execute the file created in Step 2 using the following shell command:
load("db_delete.js")
8. Use the following command to verify that the new database newDB has been removed. It should no longer show up in the list of databases:
show dbs
9. Copy and paste the output contents from Step 8 into output10-2.txt. [20 pts]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
