mongo_go 5.1.4 mongo_go: ^5.1.4 copied to clipboard
Provides a MongoDB driver based on the official GO SDK via Dart FFI meant to be used in Dart server-side code.
import 'package:mongo_go/mongo_go.dart';
void main() async {
// you can keep this as a singleton.
final connection =
await Connection.connectWithString("mongodb://localhost:27021/");
final database = await connection.database("some_database");
final collection = await database.collection("some_collection");
await collection.createOneIndex({'name': 1});
await collection.insertOne({
'name': 'Alice',
'education': 'bachelor',
});
final lst = await collection.find({'name': 'Alice'}).toList();
print(lst);
try {
await connection.withTransaction(
(session) async {
await collection.insertOne(
{'name': 'Charles', "test": "trxFail"},
session: session,
);
await collection.insertOne(
{'name': 'Bob', "test": "trxFail"},
session: session,
);
throw 'oops';
},
);
} catch (_) {
print(
'Check that neither Charles or Bob were inserted, because we threw \'oops\'');
}
final bachelors = collection.aggregate([
{
'\$match': {'education': 'bachelor'}
}
]);
await for (final batchelor in bachelors) {
print(batchelor['name']);
}
}