Creative Bracket

Dart and MongoDB Tutorial #1: Using the mongo_dart package

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 checkout the starter branch to follow along. You will learn much better this way. I know it’s convenient just watching, but doing will be much more fulfilling☺️

In the root directory of the project run pub get to update your dependencies.

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.

$ 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.

Continue watching


Sharing is caring smile emoji

If you enjoyed reading this post, please share this through the various social buttons hovering on the left/top side of the screen ↖️arrow up emoji. Also, check out and subscribe to my YouTube channel (hit the bell icon too) for videos on Dart.

Subscribe to the newsletter for my free 35-page Get started with Dart eBook and to be notified when new content is released.

Like, share and follow me heart eyes emoji for more content on Dart.

Jermaine Oppong

Hello 👋, I show programmers how to build full-stack web applications with the Dart SDK. I am passionate about teaching others, having received tremendous support on sites like dev.to and medium.com for my articles covering various aspects of the Dart language and ecosystem.

Useful learning materials



5 comments

  • Hi Jermaine. I’m one of your fans. I learn a lot from you and I want to thank you for that. Keep your good job mate.

    Sorin Neagu

  • Me again Jermaine.

    My mongod server is working, but Dart throw that error: dart_sdk.js:5822 Uncaught Error: Unsupported operation: Socket constructor
    I’m able to access the database from Compass and terminal, but not from Dart. That open() command gives the error. What could be the cause?