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
- Import the mongo_dart package / add package dependency
- MongoDB user id and password
- MongoDB name
Setup the dependency as follows and import Mongo
dependencies:
flutter:
sdk: flutter
mongo_dart: "^0.4.4"
import 'package:mongo_dart/mongo_dart.dart';
The connection string
"mongodb+srv://<user>:<password>@cluster0.cqnll.mongodb.net/<database>?retryWrites=true&w=majority");
The connection string consist of username, password and database name which required to access , (this is a access anywhere connection string) . Let create a Db instance and invoke the database.
load() async {
var db = await _MongoDB.Db.create(
"mongodb+srv://admin:text123@cluster0.cqnll.mongodb.net/projectdata?retry
Writes=true&w=majority");
await db.open();
}
To access collections you need to use db.collection as follows
var coll = db.collection('projectdocs');
Find
To get all the elements we can use find with zero arguments
var rs = await coll.find();
await rs.forEach((element) {
print(element['_id]);
}
Searching for specific documents can be achieved by using find({‘eid’:’u7867′}).
- 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 }
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.
New Document
For inserting new document to the MongoDB collection we can use insert command as follows
collection.insert({
'eid':'d345',
'name':'Mohan Kumar'
});
findAndModify ()
This method can be used to search and modify documents in the collection. Following is a sample update statement.
var pid='rt76';
coll.findAndModify(query: {
'pid': pid
}, update: {
'projectName': value.projectName,
'projectDesc': value.projectDesc,
});
This will find document with pid rt76 and update the document with new one ( passed in ‘update:’ )
Deletion
It can be performed using find and remove command. For emptying the collection we can use the remove commandFromCollection
db.removeFromCollection('projectdocs');
command which accept the a collection name.
How can i create subcolletions for a specific document like firebase?
LikeLike
array of object , may be server the purpose
https://developerm.dev/2021/01/24/how-to-add-element-to-mongoose-array-in-nodejs/
LikeLike