Appstitch MongoDB
Serverless Mongo DB Integrations for Appstitch
Platforms
- iOS
- Android
- Web
Usage
Create a document
void insertUser() async {
final user = User(
email: "[email protected]",
firstName: "Steve",
lastName: "Rogers",
dateOfBirth: DateTime(1918, 07, 04)).toJson();
final result = await db
.collection("users")
.insert(user);
if (result.success!) {
final userID = result.id!;
} else {
print(result.message!);
}
}
Fetch one document
void fetchUser() async {
final result = await db
.collection("logging")
.id(userID)
.include(
["email", "firstName",
"dateOfBirth"])
.fetch();
if (result.success!) {
user = User.fromJson(result.doc!);
} else {
print(result.message!);
}
}
Fetch multiple documents
void fetchUsers() async {
startSpinner();
final result = await db
.collection("logging")
.where("email", OperatorType.equal, "[email protected]")
.limit(20)
.fetch();
if (result.success!) {
final users = result.docs!
} else {
print(result.message!);
}
}
Read Options
| Option | Type | Description |
|---|---|---|
| collection | String | Required. Collection name |
| id | String | Return a specific document. Either 1 document is returned or null |
| where | string, OperatorType, object | A query object used filter documents |
| include | String[] | Return specific document fields. The _id field is, by default, included in the output documents. |
| exclude | String[] | Prevent specific document fields from being return. The _id field is, by default |
| limit | number | Limits the number of documents. Default 50 returned |
| startAfter | number | Skips the first n documents where n is the specified skip number and passes the remaining documents |
Update a document
void updateUser() async {
user.email = "[email protected]";
final result =
await db.collection("logging").id(userID).update(user.toJson());
if (result.success!) {
print("Successfully Updated");
} else {
print(result.message!);
}
}
Delete a document
void deleteUser() async {
final result = await db
.collection("logging")
.id(userID).delete();
if (result.success!) {
print("Successfully Deleted");
} else {
print(result.message!);
}
}
Write Options
| Option | Type | Description |
|---|---|---|
| collection | String | Required. Collection name |
| id | String | Return a specific document. Either 1 document is returned or null |
| where | string, OperatorType, object | A query object used filter documents |
Algolia Integration
The Algolia integration keeps your MongoDB & Algolia data in sync. The syncData option is available on all write operations (insert, update, delete).
Example
final result = await db
.collection("users")
.insert(_user, WriteOptions(syncData: true))
Install
appstitch_core: ^1.0.4
appstitch_mongodb: ^1.0.0
Initialize
import 'package:appstitch_core/options.dart';
import 'package:appstitch_core/core.dart';
import 'package:appstitch_mongodb/mongodb.dart';
Core core = Core();
MongoDB db = MongoDB();
@override
void initState() {
super.initState();
final options = Options(appstitchKey, clientID: clientID);
core.initialize(options);
}