MongoDB Database Guide

Finch uses mongo_dart package for MongoDB database operations. You can use the mongo_dart package to perform database operations in your Finch application.

Connect to MongoDB Database

To connect to MongoDB database, you need to add the following code to your app.dart file.

import 'package:finch/finch.dart';

void main() async {
  final configs = FinchConfigs(
    dbConfig: FinchDBConfig(
      enable: true,
      host: 'localhost',
      port: '27017',
      user: 'database_username',
      pass: 'database_password',
      dbName: 'database_name',
    ),
  );

  final server = FinchApp(configs: configs);

  server.start().then((value) {
    Console.p("Server started: http://localhost:${value.port}");
  });
}

In the above example, we have enabled the MongoDB database and specified the host, port, username, password, and database name. You can change these values to match your database configuration.

Use MongoDB Database

To use the MongoDB database, you can use the app.mongoDb property. This property returns the Db object that you can use to perform database operations.

var db = app.mongoDb;

Database Collections

Finch provides a simple way to create database collections for MongoDB database. You can use the DBCollection class to define the structure of your database collection. The DBCollection class provides a simple way to define the fields of your collection. You can use the DBField classes to define the fields of your collection.

import 'package:finch/finch.dart';

class ExampleCollection extends DBCollection {
  ExampleCollection() : super(db: app.mongoDb, name: 'example');

  Future<void> insertExample(ExampleModel model) async {
    await collection.insert(model.toJson());
  }

  Future<List<ExampleModel>> getAllExample({
    int? start,
    int? count,
  }) async {
    start = (start != null && start > 0) ? start : null;
    var rows = await collection
        .modernFind(
          limit: count,
          skip: start,
          sort: DQ.order('_id'),
        )
        .toList();
    return ExampleModel.fromListJson(rows);
  }
}

In the above example, we have defined a collection named example with two fields: title and slug. The title field is defined as a string and is set as the primary key. The slug field is defined as a string.

You can use the DBCollection class to create collections for your application. You can also use the DBCollection class to create relationships between collections. For example, you can define a foreign key relationship between two collections.

Querying Data

Finch provides a simple way to query data from your MongoDB database. You can use the modernFind method to query data from your collection. The modernFind method provides a simple way to query data from your collection. You can use the DQ class to build your query.

var rows = await collection
    .modernFind(
      limit: count,
      skip: start,
      sort: DQ.order('_id'),
    )
    .toList();

Example

For more information, you can check the example project in the Finch repository. The example project uses MongoDB database and Finch's database features.