This is quick start for developers who want to be up and running in no time.
The 3 steps:
- Setting up the environment, MongoDB Server, installing locally[recommended]
- Seeding Sample dataset using this repository[recommended]
- MongoDB shell cheat sheet, to Start Practicing the commands.
- ๐ Getting Started
- 1. ๐ง Setting up the environment
- 2. ๐ Sample Seed Data-set
- 3. ๐MongoDB shell cheat sheet, to Start Practicing the commands.
- โ๏ธ Built Using
- โ๏ธ Authors
While following along the guide, I recommend to install MongoDB locally on the system and use this repository to seed data. So that the we can follow the examples using the cheat sheet provided.
In order to work with mongo db we need to setup an environment.
We need access to a Mongo DB server.
- Installing Mongo DB locally on the system [Recommended]
- Using the on-line database-as-a-service(daas) Mongo DB
- Other Hosting mongo DB instance on-line e.g. AWS etc.
- MongoDB on-line REPL
There is a community edition that we can install for free. The original documentation shows how to install on each system, macOS, Windows and Linux systems. MonoDB installation
Here are a few points worth mentioning:
- In Windows we can install the MongoDB server as a service, so it can automatically run on Windows load.
- In Windows, we can also use chocolatey or scoop to install it.
https://medium.com/@zibon/installing-mongodb-in-Windows-like-ubuntu-5d6d2285dac6
Here is the one for scoop:
https://gist.github.com/basir/a812d6870c12d8bd2d53981bbeeefa7c
MongoDB provides on-line database service as Atlas. We can sign-up for free to get a dabatase instance.
MongoDB Atlas OR MongoDB Atlas from mlab, 512-Mb free space.
We can refer to this article to see what options we got to host your own mongo DB server. cheap-free-mongodb-hosting
Here are a few worth mentioning:
https://www.jdoodle.com/online-mongodb-terminal/ https://www.mplay.run/mongodb-online-terminal https://mongoplayground.net/
If we need some comprehensive, dynamic dataset we can use either of the following:
Here is a summary of all the commands, However, read all to complete the configuration before using these commands.
clone https://github.com/planetapex/mongo-db-cheat sheet.git
npm install
node seed1.js
We can create our own datasets in a matter of minutes, just read about faker.js and make changes in seed1.js.
However, to get along the examples set out, please use the dataset as it is:
Clone the repository using:
clone https://github.com/planetapex/mongo-db-cheat sheet.git
Goto the repository directory on your system and run the following command:
npm install
This will install all the necessary npm packages.
Now, we need to configure our db string in order to connect to the database and seed the data.
Edit the file in config.db.js and put the relevant configuration:
Lastly, there is a variable noOfRecords defined for the number of documents to generate. Initially its value is set to 10,000. You can change it to your liking. However, A very high number in the millions would take some time to process, so please be patient.
The repository contains 3 files for seeding the data. All the 3 files seed the same data. So, We can use any one of them. Run any one of the following commands to seed the data.
node seed1.js
node seed2.js
node seed3.js
There are plenty of data available for use as a sample dataset:
checkout the following for an idea: Question for MongoDB datasets
Clone or Get the JSON file from the following repository, MongoDB sample JSON files and run the following command:
mongoimport --db test --collection zips --file d:\sample\zips.json
Clone this repository Northwind CSV db
In Windows Powershell, Go to the repository directory and run the following commands:
Get-ChildItem "E:\temp\nw" -Filter *.csv | `
Foreach-Object {
C:\Program_Files\MongoDB\Server\4.2\bin\mongoimport.exe -h localhost:55000 -d northwind -c $_.BaseName --type csv --file $_.FullName --headerline
}
NB: The options -h localhost:55000 -d northwind, is the db string pieces.
Ususally, when you follow the examples on mongo DB documentation website, they provide you with some dataset to work with.
The original documentation even gives sample dataset to work with:
https://medium.com/dbkoda/mongodb-sample-collections-52d6a7745908
Go to the mongo db bin folder and ,
- If mongo db server is installed on local system, just type monogo and we will be connected.
- If using a database-as-a-service(daas)instance wanting to connect using mongo shell
Here is the cheat sheet to get started in no time.
- M.Yasir Ali Shah@planetapex - Idea & Initial work