Mongodb drop and create collection
MongoDB is an open-source NoSQL database which means that, unlike relational databases, it does not accept input values in table format. Data is stored in collections and documents since MongoDB is a document-oriented database. Rows in an SQL table have been replaced with documents in MongoDB. Show
This article assumes that you’ve already installed the MongoDB server on your computer and connected a shell to the server. If you have already done so, we can explore a few features of MongoDB but first, a few terminologies: If not, you can check out the article on how to install MongoDB on Ubuntu.
{
Creating a MongoDB databaseDatabase creation in MongoDB happens implicitly when you try to use a database. To create a database, type the following in the mongo shell; > use fossDB Output: Create databaseNote: To open the Mongo shell, run the command below: mongo MongoDB will first check to confirm if a database named fossDB is present. If not, it will then create a new one to be used. The Mongo shell then switches to fossDB. This means that every collection and document created, updated, or read will be from this database unless specified otherwise. To print what database you are in right now, you use the command > db. To list all the databases available and created, you use the command >show. An example of these commands in use are shown below; >db Output: Show current databasesNote: Do not interfere with the admin and config databases as Mongo uses them for administrative purposes. Creating a MongoDB collectionTo create a collection, first, make sure that you are in the correct database that you intend to create the collection in. There are two ways in creating a collection which are: 1. Creating a collection explicitlyUse the command below: >db.createCollection("Collection1"); Output: Create collectionThis command will then create a collection named Collection1 2. Inserting a document into a new collectionYou can quickly try inserting a document into a non-existing collection. This will prompt Mongo to create a new collection for you. Do note that while this is convenient in terms of programmatically creating collections, if you are using Mongo shell and make a typo somewhere while inserting a document, the document may end up in a new database that is unbeknownst to you. The syntax for creating a new collection is; db.collection_name.insert(document); To create a collection Collection2 in fossDB database, use the following command: > db.Collection2.insert({name: "Alex",key: "value",age: 20}); Output: Create a new collection and insert dataIn this example, the document part is represented by the following JSON string: { These are the key-value pairs typical of a JSON string. The “name” is the key, and “Alex” is the value. A user can have multiple documents in this collection with the key name and a different value, say Max. Use the command below to list all the collections inside a database: > show collections Output: Display collectionsFrom the output, you will notice that both collections have been created. You are confident that you can add a new document into a collection. Showing collectionsIf you have not noticed, we have been using the show keyword quite a lot while discussing the other commands. To recap on this, the command for showing collections and databases are: >show collections Output: Shows databases and collectionsIn conjunction with the command db, these commands are used to print the current database and are pretty handy while interacting with the Mongo shell. Dropping MongoDB Collections and DatabaseThe drop command is a keyword that we have not touched on in this article. It is a command used to remove collections or entire databases from a user’s Mongo server. To drop, the following syntax will take you through the process. 1. Dropping collectionsWe will eliminate the collection “Collection2” that we created earlier. This is done by using the command below: >db.Collection2.drop() Output: Drop collectionsTo verify that the collection has been deleted, you can use the show collections command to list the remaining collections. You will notice that there will be one collection missing from the list. 2. Dropping DatabasesBefore you run the command to drop the database, you should check that you are in the correct database, or else you might get rid of the wrong database and end up losing Valuable data that you did not intend to delete. In this example, we will be dropping the database fossDB that we had created earlier. Let’s make sure that we are in the correct database using the command below: >db Output: Databaselet’s then drop the database using the command below: >db.dropDatabase(); Output: Drop databaseBelow we are going to present various SQL terms and their corresponding MongoDB terms;
ConclusionMongoDB has attained massive popularity in the developers’ world because of the JSON-like representation, scalability, ease, and dynamic way of creating documents. This article has discussed the three commands used in the MongoDB shell. We hope that this article has helped you understand these commands better. If you are looking for a database to be used in a project for data-heavy lifting, MongoDB is a good option that you might want to consider. How do I create and drop a collection in MongoDB?4 Answers. Create db mydb : > use mydb switched to db mydb.. Create a collection mycollection : > db. createCollection("mycollection") { "ok" : 1 }. Show all the collections there: > db. ... . Insert some dummy data: > db. ... . Make sure it was inserted: > db. ... . Delete the collection and make sure it is not present any more: > db.. How do I drop a collection in MongoDB?First, check the available collections into your database mydb.. >use mydb switched to db mydb >show collections mycol mycollection system. indexes tutorialspoint > Now drop the collection with the name mycollection.. >db. mycollection. drop() true > ... . >show collections mycol system. indexes tutorialspoint >. What is db collection drop ()?In MongoDB, db. collection. drop() method is used to drop a collection from a database. It completely removes a collection from the database and does not leave any indexes associated with the dropped collections.
What is the difference between drop and delete in MongoDB?DELETE is a Data Manipulation Language command, DML command and is used to remove tuples/records from a relation/table. Whereas DROP is a Data Definition Language, DDL command and is used to remove named elements of schema like relations/table, constraints or entire schema.
|