Tag Archives: mongoDB

Mongo with mongoose

Connect Flutter app with MongoDB


To connect and perform CURD operations use any driver package available on pub.dev. mongo_dart package offer everything we need and it is pretty popular than others.

A prerequisite for using the dart package is that configure Mongo Cluster.  

Mongo_dart package

Mongo dart package is one of the most used one for connecting MongoDB with dart. You can grab the connection string from the MongoDB or just use the given code and put it in an async function.

For connecting MongoDB we need

  1. Import the mongo_dart package / add package dependency
  2. MongoDB user id and password
  3. MongoDB name

Continue reading

Configuring MongoDB cluster for Flutter


MongoDB is NoSQL database just like Firebase, Raven ect. It will store data as Json structure / Map with key value pair. NoSQL offer great flexibility coupled with speed.

Flutter app with an cloud database will be awesome . The simplest way to try with cloud database is Mongo or Firebase , both have free tier plans. Visit MongoDB  and create  a cluster (start with 512 MB free tier) and also Database and collection.

So How do we make MongoDB ready for flutter ?. We can use any package from pub.dev ( mogno_dart is recommended)  which is a driver to access ( just like database driver for SQL on .Net or any other language) Mongodb cluster  database and collections for performing CURD operations.

Before that we have to configure Network Access on Mongo cluster. Mongo cluster can be enabled for particular IP address or you can enable anywhere access mode by entering  0.0.0.0/0  in Network Access of mongo cluster. Other wise your Flutter connection never  succeed.

Then you can grab the connection string using the Wizard and goon with mongo_dart.

Mongo with mongoose

Find documents by objectId using mongo_dart package in Flutter


Mongodb ObjectID (_id) is a Bson object which can be used search documents, and also for update and delete. The _id is unique and contain many properties like timestamp etc. We can use it on mongo console as follows

db.todos.find()
{ _id: ObjectID("5f9ae45b6acbd21070cb75d8"),
  item: 'Write next tutorial',
  __v: 0 }
{ _id: ObjectID("5f9ae4776acbd21070cb75db"),
  item: 'Pay insurance premiums',
  __v: 0 }
{ _id: ObjectID("5f9ae4856acbd21070cb75dc"),
  item: 'Reserve movie tickets for Monday',
  __v: 0 }
{ _id: ObjectID("5f9d7b49f38c36015cacf0d0"),
  item: 'create a tutorial - express app in a minute',
  __v: 0 }
{ _id: ObjectID("5fa0ba682bee610055bfaf6a"),
  item: 'something',
  __v: 0 }
{ _id: ObjectID("5fa6d7c4f6bd2400550bf125"),
  item: 'asdad',
  __v: 0 }
{ _id: ObjectID("5fa6da2df6bd2400550bf126"),
  item: 'setup a flutter mongodb',
  __v: 0 }
db.todos.find({'_id': ObjectId("5fa6da2df6bd2400550bf126")})
{ _id: ObjectID("5fa6da2df6bd2400550bf126"),
  item: 'setup a flutter mongodb',
  __v: 0 }
MongoDB

Mongo Compass Console

In Flutter

In Flutter you have the Mongo Dart package to access the MongoDB, which offer same type of CURD commands. While you use the following format of find function you may catch the errors.

var coll=db.Collections('todos');
var docs=coll.find({'_id': 'ObjectId("5fa6da2df6bd2400550bf126")'});

Solution

As I told before the hexa string “5fa6da2df6bd2400550bf126 is a Bson object.  To use it with find/update we have to use ObjectId instead of  hexa string.  The dart package also provide ObjectID class to convert the string  into ObjectId type and then we can use it as follows

ObjectId objId = _MongoDB.ObjectId.parse("5fa6da2df6bd2400550bf126");
var docs=coll.find({'_id': objId});

and it will work as expected.

Mongo with mongoose

Work with MangoDB using mongoose in Nodejs-Express app


MangoDB is a NoSQL database which can be backend for web/mobile application. Working with Nodejs Express app, what I feel is Python Flask like environment, rendering, templating etc everything in place.

Mongoose

Mongoose is MangoDB modeling tool, which is designed to work with asynchronous environment (none blocking code). The following is required to use mongoose in your app

  1. mongoose module
  2. Connection string
  3. Schema
  4. Model

Install Mongoose

Install mongoose  from npm library by executing command on the terminal

npm install mangoose -save  *

*The save option will add mongoose into dependecies section of package.json (do you know, can install all dependencies of  project by firing npm install command on shell)

Continue reading