Creating and populating a new MongoDB

Open a new terminal session and start the MongoDB CLI.

mongo

At the command prompt > enter the following command to create a new database named development.

> show databases
admin   0.000GB
config  0.000GB
local   0.000GB
> use development
switched to db transactions
> show databases
admin        0.000GB
config       0.000GB
development  0.000GB
local        0.000GB

If the database does not already exist it will be created for you.

Now that we have a database created we need to create a collection and insert some documents into it so we can use for later testing/development.

> db.createCollection("transactions")
> show collections
transactions
> db.transactions.insert({
    accountId: 'A00000001',
    description: 'Bowers & Wilkins P5 Headphones',
    amount: 299.00,
    currency: 'GBP',
    dateCreated: new Date(2020,1,9,14,30)
})

We can now query the document we have inserted into our new collection:

> db.transactions.find().pretty()
{
	"_id" : ObjectId("5e6693f2313ea58d23458752"),
	"accountId" : "A00000001",
	"description" : "Bowers & Wilkins P5 Headphones",
	"amount" : 299,
	"currency" : "GBP",
	"dateCreated" : ISODate("2020-02-09T14:30:00Z")
}