In this video series, we will learn how to talk to a MongoDB database with Dart, using the mongo_dart package. MongoDB is a well-known NoSQL database that stores its items as JSON documents (BSON to be exact). At the end of this video, you will be comfortable setting up a MongoDB database, connecting to your database, creating your own collections and performing CRUD operations.
Prerequisites
Download and Install MongoDB to use the database. Alternatively use the official docker image.
Checkout the starter branch to follow along. You will learn much better this way. I know it’s convenient just watching, but doing so will be much more fulfilling.
In the root directory of the project update your dependencies:
$ pub get
Setup your database
Run Mongo server running using the startup commands on the documentation page for your Operating System.
Log in to MongoDB by entering mongo
in the terminal. Create a database and collection:
> use test;
switched to db test
> db.createCollection('people');
{ "ok" : 1 }
> exit
bye
Upload the people.json
file into the people
collection we just created. This file is in the root project.
$ mongoimport --jsonArray -d test -c people --file people.json
... connected to: localhost
... imported 100 documents
To confirm all is good, log in to Mongo and check the collection:
$ mongo
...
> use test
switched to db test
> show collections
people
> db.people.find();
[{ "id": 1, "first_name": "Nolly", "last_name": "Montfort", "email": "nmontfort0@archive.org", "gender": "Male", "ip_address": "6.125.126.90" },
...
...
]
Good job! Let’s continue with the video.