How to create a database and add a collection with MongoDB GUI Compass

Jack Wallen shows you how easy it is to create a MongoDB database and collection using the Compass GUI.

Image: Shutterstock

Now that you have the MongoDB Compass GUI installed, you might be wondering what you can do with it? The answer is pretty simple—just about anything you need. The Compass GUI makes it very easy to create and manage your MongoDB databases.

SEE: Hiring Kit: Database engineer (TechRepublic Premium)

I want to show you just how easy it is to create a MongoDB database and then add a collection. For those who don’t know, a collection is a NoSQL term that refers to a group of MongoDB documents. Collections are to MongoDB what a table is to a relational database (such as MySQL).

Let’s dive right into this.

What you need

I will assume you’ve already deployed MongoDB and Compass (read the how-to in the link above for more information). With those two pieces of the puzzle taken care of, it’s time to create your first database.

How to create a database with Compass

The first thing we’ll do is create a database that will house our collection. Log into Compass and then click the Databases entry in the sidebar (Figure A).

Figure A

The Compass GUI makes working with your databases incredibly simple.

In the resulting window, click Create database. You will then be prompted to name your new database (Figure B).

Figure B

Naming our new database in Compass.
Naming our new database in Compass.

Not only will you be asked to give your new database a name, but also a collection name. This will create an initial collection in your new database. The initial collection is a requirement, but you are not limited to a single collection in the database.

After the database is created, click on it in the database listing and you’ll see the initial collection listed (Figure C).

Figure C

We've created our first database along with an initial collection.
We’ve created our first database along with an initial collection.

You can then create a new collection by clicking Create collection, which will prompt you to give the new collection a name (Figure D).

Figure D

Naming a new collection for our database.
Naming a new collection for our database.

Click Create Collection and you’ll be sent back to the database window.

How to create a document in a collection

The next step is to create your first document within a collection. To do that, click the name of the collection you want to work with to reveal the collection interface (Figure E).

Figure E

We can now add documents to our new collection.
We can now add documents to our new collection.

Click the ADD DATA drop-down and then select Insert Document. This is where Compass veers away from the simple. Instead of offering a handy GUI interface to add a document to the collection, you have to type (or paste) the entry manually. Here’s a sample document to be added to a published collection in the books database. Let’s say we want to insert a single document into the collection. The document code will look like this:

"title" : "The Haunting of Lilly Marchbanks", "author" : "Jack Wallen", "published" : "yes"

You would add the above code in the editor, replacing the default line:

* Paste one or more documents here

So when you click Insert document, the editor will open (Figure F).

Figure F

The Compass document editor makes it easy to copy and paste your entries.
The Compass document editor makes it easy to copy and paste your entries.

To enter (or paste) your document line such that it looks like:

{

"_id": {

"$oid": "624f0977e276a6c6caf108c2"

},

"title": "The Haunting of Lilly Marchbanks"",

"author": "Jack Wallen",

"published": "yes"

}

Compass will automatically assign a random string to the _id field, so you won’t have to bother with that.

Once you’re finished, click Insert and the document will be added to the collection. One very handy thing you can do next is to clone a document. Once you’ve cloned a document, you can easily change the values for the fields.

To clone a document, click the Clone icon (directly to the left of the trash icon) associated with the document in question (Figure G).

Figure G

Getting ready to clone a document within a collection.
Getting ready to clone a document within a collection.

Edit the values in the entry (Figure H), and click Insert.

Figure H

Creating a new document from a clone.
Creating a new document from a clone.

You can also import a document into a collection. Create the document in JSON format like so:

[

{

"title": "I Zombie I",

"author": "Jack Wallen",

"published": "yes"

},

{

"title": "Klockwerk Kabaret",

"author": "Jack Wallen",

"published": "yes"

},

{

"title": "Defying Gravity",

"author": "Jack Wallen",

"publisehd": "yes",

"publisher": "Devil Dog Press"

}

]

Save the file with a name like compass_import. Within Compass, click the ADD DATA drop-down and select Import File. In the resulting window (Figure I), click JSON and then click Select a file. Navigate to (and select) the compass_import file and then click IMPORT.

Figure I

Importing a file into Compass.
Importing a file into Compass.

When the import is finished, click DONE and you should see all of your new documents added.

For those looking to make working with documents in Compass easiest, the best method is using the Import feature.

And that’s all there is to using the MongodB Compass GUI to create databases and documents. This database management system is a great option for those looking to get into housing massive stores of data with very complex relationships.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

For all the latest Technology News Click Here 

 For the latest news and updates, follow us on Google News

Read original article here

Denial of responsibility! TechNewsBoy.com is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – [email protected]. The content will be deleted within 24 hours.