Here is a sample I have used for testing in Mongo.
This creates a simple database with a few book entries:
db = connect("library")
db.books.insertMany([
{
title: "The Last of the Mohicans",
author: { forenames: "J Fenimore", surname: "Cooper" },
genre: "Fiction",
quality: 5
},
{
title: "The Lord of the Rings",
author: { forenames: "JRR", surname: "Tolkien" },
genre: "Fiction",
quality: 5
},
{
title: "A la Recherche du Temps Perdu",
author: { forenames: "Marcel", surname: "Proust" },
genre: "Biography",
quality: 5
},
{
title: "Parade's End",
author: { forenames: "Ford Maddox", surname: "Ford" },
genre: "Fiction",
quality: 5
},
{
title: "The Good Soldier",
author: { forenames: "Ford Maddox", surname: "Ford" },
genre: "Fiction",
quality: 5
},
{
title: "The Moon's a Balloon",
author: { forenames: "David", surname: "Niven" },
genre: "Biography",
quality: 5
}
])
The first thing you need is to create a library folder and start the Mongo server. NB you must do this in a separate process.
mkdir library
C:\Program Files\MongoDB\Server\5.0\bin\mongod --dbpath library
You can then execute the above script by the following commands:
C:\Program Files\MongoDB\Server\5.0\bin\mongo
use library // the name of the database
load("scriptname") // the name of the script provided above