Nov 30, 2020
Accessing MongoDB Purely via Dart
Performing basic CRUD operations on your MongoDB database locally using the 'mongo_dart' package.
Author


Book a call
These requirements can be fulfilled by using a NoSQL database and having a powerful database like MongoDB really comes in handy.
Why use MongoDB?
Together, Dart and MongoDB can be seen as a killer-combination for developers!
Dart & MongoDB
Let’s get straight to business!
What we will be trying to achieve in this demo is to make a basic API that will send a request to the server and the server in turn will process that request and perform basic CRUD operations on the database based on the type/parameters of the request. We will only work with Dart programming language for making the API and to interact with the database (Dart is not just for making beautiful UI’s for mobile and web, it can do the backend work too!).
After you install MongoDB, you can type the
mongo command in the terminal. It will log you into the mongo shell and from here you can interact with your database locally. The show dbs command can be used to list out the locally available databases to you. To select a database to work with you can use the command use, it will start using that database as the current database or will create one if the database is not already present. You can also check the current database by typing the db command in the mongo shell. It will return the name of the currently used database.
> show dbs admin 0.000GB config 0.000GB dart_test 0.000GB local 0.000GB test 0.000GB > use dart_test switched to db dart_test > db dart_test
mongod command (mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access and performs background management operations). Then, import the people.json file in your database using the command:
mongoimport --jsonArray -d yourDatabaseName -c yourCollectionName --file path/To/File.json
You can check whether the JSON file is properly imported by running this command in the mongo shell:
db.yourCollectionName.find()
Let’s begin with the implementation.
Interaction With DB
pubspec.yaml file and run the command flutter packages get to be able to use this package.Db class of the package by passing the URL of our database as a constructor parameter. open() method on it.That's it. Now we are connected to our database.
http_server package that allows us to handle the HTTP requests made to the server.We will create a server and start listening to a particular port where all the requests will be made. For this demo, we will be using /people endpoint to make requests to the server (You can use any available port for listening).
/people endpoint, it instantiates a PeopleController class, which takes the request body and the database reference as the arguments to its constructor. This class can be considered as a controller class that handles the request. It is considered good practice to separate your logical component of the code for a better structure.The constructor of the PeopleController class invokes the handle() which in turn calls handleGet(), handlePost(), handlePut(), handleDelete() based on the type of request passed to the function. Here, _store refers to the particular collection name which we have in the database and _req is the entire request object that we have sent while sending the HTTP request.
This method will be invoked when we make a
GET request to the /people endpoint. We can either get all the documents of the collection or just a particular document based on the id passed as a query parameter in the request. If there is no id passed in the query parameter, we can simply return all the documents from the collection by calling the find() method on the collection. If there is a particular id passed as the query parameter, we can call the findOne() function on it which takes a selector as an argument.In the selector, we can add some conditions to filter out data based on our requirements.
handlePost(), handlePut() and handleDelete() functions. The complete source code is available at Github: https://github.com/SamvitChrungoo/dart_mongo_example
Just run the main.dart file using the command:
dart main.dart
You are now listening to the particular port you have targeted in the code. You can make requests to
http://localhost:portno/people using Postman or curl. Play around with the request to test the above API and see your database changing locally.
Congrats !! You have implemented an API that interacts with MongoDB purely using Dart programming language.
Bonus Tip:
- Import and export in CSV, JSON, SQL and BSON/mongodump
- Write SQL to query MongoDB
- Auto-complete collection names, field names & more in the shell
- Use a drag-and-drop UI to build queries
- Find outliers in your data and errors in your schema
Thanks for reading. Until next time !! 👋 👋
Subscribe to Our Newsletter
Subscribe to RSS
Press & Media Hub RSS FeedRelated Articles.
More from the engineering frontline.
Dive deep into our research and insights on design, development, and the impact of various trends to businesses.

Jun 27, 2026
Building a Resilient Hybrid-Cloud Network with WireGuard HA, Route-Based Failover, and Deep Observability

Jun 19, 2026
We Built a 114-Second AWS-to-Azure Failover. Here’s What We Learned

Jun 12, 2026
Cloud-Native and Cloud-Agnostic Are Not Ideologies; They Are Business-Stage Decisions

Jun 8, 2026
Geeklego: The Open-Source Design System Built to Work With AI

May 18, 2026
Your Vibe Code Has No Memory. DESIGN.md Fixes That.

May 14, 2026