1

I want to use mongo atlas in my flutter project and currently, I am using the mongo_dart package and I am able to add documents directly from my app.

var db = await Db.create("mongodb+srv://<username>:<password>@cluster0.cwdcc.mongodb.net/<database>?retryWrites=true&w=majority");
await db.open();

var coll = db.collection('Users');

Recently I saw someone saying that I need to create API and it is not good to use MongoDB like this. Can you explain Why is that I need to create an API?

Syed Rajin
  • 61
  • 1
  • 9

1 Answers1

1

So based what you saying here i.e why it is not good to connect to MongoDB without an API?

So I am to believe I stand to be corrected:

  • So mongoDB atlas before even connecting to it you have to authenticate yourself

  • Mongo will give you a connection string like this below:

mongodb+srv://username:<password>@clustername.eoxer.mongodb.net/<dbname>?retryWrites=true&w=majority

  • So it is much safer when then connection is an authenticated connection which mongoDB supports for the languages that they specified for their driver that they have

  • So with flutter if you look at this solution given here:How to connect flutter with MongoDB it seems like it is not an authenticated connection which makes it less secure

  • So you are saying it is totally fine to proceed further without creating an API in my case? – Syed Rajin Nov 09 '20 at 08:27
  • No I am answering why its not safe using the a non-authentication based connection, as for should you continue with this route personally I would said no reason being it's not safer approach of securing your data. I would thus advise if you do not know any language that mongo supports to use their driver I would say use firebase that is secure – Ntshembo Hlongwane Nov 09 '20 at 08:32
  • I am afraid it might get expensive when it has a lot of data? Will it be a good choice for social media app? – Syed Rajin Nov 09 '20 at 08:35
  • Then if this a project that you then want to ship in to production and not just a play project then I would advise you learn how to use one of the mongo drivers supported by mongo. You have ```NodeJS, Python, Ruby, C, C++, C# / .Net``` and many more you just have to go mongoDB site and see then choose whatever that you prefer and start learning it – Ntshembo Hlongwane Nov 09 '20 at 08:39
  • Is there any other database that will be better for social media that works with flutter? – Syed Rajin Nov 09 '20 at 08:42
  • Firebase is the only database that I could suggested right now check out their docs https://firebase.google.com/docs/flutter/setup I hope my answer was what you want and solves your queries in mind :D – Ntshembo Hlongwane Nov 09 '20 at 08:44
  • If you go with cloud firestore for a social media, there are hell lot of drawbacks. Complex queries are a headeche and the most important part searching. For searching anything like user names, you have to depend upon Algolia / ES which gets expensive over time. – Rohan K Feb 14 '21 at 04:36
  • @Ntshembo Hlongwane it is quite unclear what you mean with "non-authentication based connection". If it is no authenticated, Atlas doesn't let you in. – J F Apr 03 '21 at 11:24
  • Hello, I know I am a bit late to this question, but I have a question? In mongo_dart Documentation, it says that you can add tsl=true or ssl=true and secure the connection in that way. I need the .watch() functionality implemented because I want to listen for realtime changes. I have tried using it with my Flask API and Socket.io, but I really don't understand how it works. What would your recommendation be @ Ntshembo Hlongwane? – bd_28 Sep 12 '21 at 11:37