In this article, we will cover how to create a database using the MongoDB Atlas UI (User Interface), the MongoDB Shell (mongosh), or using Compass, the MongoDB GUI. All methods are available for MongoDB Atlas, MongoDB’s developer data platform, and for self-managed clusters. Show Table of ContentsUsing the MongoDB Atlas UIThe easiest way to get started with MongoDB is by using the Atlas developer data platform. A free tier is available with basic database functionalities. This free tier is more than enough for the purposes of this article. Prerequisites for MongoDB AtlasTo create a database on MongoDB Atlas, you will need to register an Atlas account and create your first forever-free cluster: Creating a MongoDB Database with the Atlas UIFrom your cluster page, click on “Browse Collections.” If there are no databases in this cluster, you will be presented with the option to create your first database by clicking on the “Add My Own Data” button. This will open up a modal, asking you for a database name and collection name. Once these fields are filled, click on “Create” and your database will be created for you. The database is now available to you. You can manually enter new documents, or connect to the database using any of the MongoDB drivers. Using the MongoDB ShellLike most complex software systems, MongoDB can be controlled with what is called a command-line interface, often referred to as a CLI. By entering commands into the CLI, you tell MongoDB how to operate, get information about how the MongoDB cluster is running, and perform fundamental actions like the one we will cover today: creating a database. To create a database using a command-line interface, the first task is to get access to the MongoDB cluster you are using via the MongoDB Shell. A shell is a program that allows you to enter commands into a software system. Prerequisites for using the CLI with MongoDB AtlasIf you are using MongoDB Atlas, the steps to getting a shell are as follows:
Find out more at Connect to Atlas via MongoDB Shell Prerequisites for using the CLI with a self-managed MongoDB clusterIf you are running a self-managed cluster of MongoDB:
Find out more at Connect to a Deployment from the MongoDB Shell Creating a MongoDB database with the CLIOnce you have access to a cluster via the MongoDB Shell, you can see all the databases in the cluster that you have access to using the “show” command:
Note that and local are databases that are part of every MongoDB cluster. There is no “create” command in the MongoDB Shell. In order to create a database, you will first need to switch the context to a non-existing database using the use command:
Note that, for now, only the context has been changed. If you enter the show dbs command, the result should still be the same:
Wait a second. Where’s myshinynewdb? MongoDB only creates the database when you first store data in that database. This data could be a or a . To add a document to your database, use the db.collection.insert() command.
The “user” in the command refers to the collection that the document was being inserted in. Collections can be created just like databases, by writing a document to them. They can also be created using the createCollection command.
Now, if you run the show
There’s one more thing. How did the insert command know to put the data into myshinynewdb? It turns out that when you entered the use command, then myshinynewdb became the current database on which commands operate. To find out which database is the current one, enter the 0 command:
The 0 command displays the name of the current database. To switch to a different database, type the use command and specify that database.Using the GUI, MongoDB CompassSome users would rather work with a GUI to create and update their data and collections. The MongoDB GUI, Compass, offers additional functionality like data visualization and performance profiling as well as offering CRUD (create, read, update, delete) access to data, databases, and collections. Find out more at MongoDB Compass: The Easiest Way to Manage and Explore Your Data Prerequisites for using Compass with MongoDB AtlasIf you are using MongoDB Atlas, the steps to getting to Compass are as follows: Prerequisites for using Compass with a self-managed MongoDB clusterIf you are using self-managed MongoDB:
Creating a MongoDB database with CompassThe Databases tab in MongoDB Compass has a "Create Database" button. In MongoDB Compass, you create a database and add its first collection at the same time:
The next step is to insert one or more documents into your database. Click on your database’s name to see the collection you created, then click on the collection’s name to see the Documents tab: Click the "Add Data" button to insert one or more documents into your collection. You can add JSON documents one at a time, or add multiple documents in an array by enclosing comma-separated JSON documents in square brackets, as shown in this example:
Click "Insert to add" the documents to your collection. Learn more about MongoDBUsing these simple steps shows how to create a database, a collection, and insert documents. Now you have a database, collection, and documents to work with and can learn even more. Join the MongoDB community for forums, webinars, and free courses from MongoDB University. Apakah MongoDB termasuk database?MongoDB adalah database yang berorientasi pada dokumen. Data apa pun yang disimpan di dalam MongoDB itu berbentuk dokumen, bukan tabel. Dalam dokumen-dokumen tersebut, datamu disimpan dalam field, bukan baris atau kolom. Hal ini juga yang mendukung fleksibilitas MongoDB.
MongoDB digunakan untuk apa?3. Cocok Untuk Menampung Data yang Bervariasi
Dynamic schema membuat MongoDB cocok untuk menampung data yang bervariasi baik digunakan untuk menyimpan data yang terstruktur ataupun yang tidak terstruktur.
MongoDB menggunakan bahasa apa?MongoDB sendiri ditulis dengan bahasa C++ dan telah tersedia untuk berbagai jenis bahasa pemrograman. Fitur utama dari mongoDB antara lain: model document-oriented storage.
Kapan harus menggunakan MongoDB?Dari sisi struktur data, MongoDB cocok digunakan untuk data yang tidak terstruktur.
|