MongoDB: Create new Database and Collection

I’ve recently been teaching myself MongoDB, comming from a releational database background it’s taking me time to learn how MongoDB works.  This article will be the first of many where I will post my notes and how to’s from my MongoDB learning journey.

this article is brief and in this I give an overview of MongoDB and how to create a DB and a Collection.

In MongoDB a record is called a document and is composed of field value pairs (Similar to JSON objects).  A Document is then stored in a collection, a collection is analogous to tables in releational databases.  The data records in documents are stored as BSON.

To Create a new DB and collection with a document we can do the following after you have connected to the mongo shell.

the use myDB  tells MongoDB that we wish to use this Database, even if it does not exist yet.

the db.myNewCollection.insertOnce( { x: 1 })  this will create the new DB called myDB  and a new collection with a document record called myNewCollection.

to view the collections on the DB issue the command db.getCollectionInfos()

Stay Tuned for my next article, as I continue on this MongoDB journey.

Leave a Reply

Your email address will not be published. Required fields are marked *