Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
Sync Data

Stream Data to Atlas - Node.js SDK

Puede usar Ingesta de datos para transmitir datos desde la aplicación cliente a una aplicación Atlas App Services habilitada para Flexible Sync.

You might want to sync data unidirectionally in IoT applications, such as a weather sensor sending data to the cloud. Data Ingest is also useful for writing other types of immutable data where you do not require conflict resolution, such as creating invoices from a retail app or logging application events.

Data Ingest is optimized to provide performance improvements for heavy client-side insert-only workloads.

1

Los objetos asimétricos sincronizan datos unidireccionalmente. Define un objeto asimétrico configurando asymmetric a true en su modelo de objeto:

class WeatherSensor extends Realm.Object {
static schema = {
name: "WeatherSensor",
// Sync WeatherSensor objects one way from your device
// to your Atlas database.
asymmetric: true,
primaryKey: "_id",
properties: {
_id: "objectId",
deviceId: "string",
temperatureInFahrenheit: "int",
barometricPressureInHg: "float",
windSpeedInMph: "float",
},
};
}
class WeatherSensor extends Realm.Object<WeatherSensor> {
_id!: Realm.BSON.ObjectId;
deviceId!: string;
temperatureInFahrenheit!: number;
barometricPressureInHg!: number;
windSpeedInMph!: number;
static schema: ObjectSchema = {
name: "WeatherSensor",
// sync WeatherSensor objects one way from your device
// to your Atlas database.
asymmetric: true,
primaryKey: "_id",
properties: {
_id: "objectId",
deviceId: "string",
temperatureInFahrenheit: "int",
barometricPressureInHg: "float",
windSpeedInMph: "float",
},
};
}

For more information on how to define an asymmetric object, refer to Define an Asymmetric Object.

2

To stream data from the client to your backend App, you must connect to an App Services backend and authenticate a user.

// Create an anonymous credential
const credentials = Realm.Credentials.anonymous();
const user = await app.logIn(credentials);
// Create an anonymous credential
const credentials = Realm.Credentials.anonymous();
const user = await app.logIn(credentials);

Data Ingest es una funcionalidad de Flexible Sync, por lo que la aplicación a la que te conectes debe usar Flexible Sync.

3

After you have an authenticated user, you can open a synced realm using a Flexible Sync configuration object.

const realm = await Realm.open({
schema: [WeatherSensor],
sync: {
user: app.currentUser,
flexible: true,
},
});

A diferencia de la sincronización bidireccional, Data Ingest no utiliza una suscripción Flexible Sync.

You cannot query an asymmetric object or write it to a local realm, so asymmetric objects are incompatible with bi-directional Flexible Sync, Partition-Based Sync, or local Realm use.

4

Once you have an open Realm, you can create an asymmetric object inside a write transaction using Realm.create(). When creating an asymmetric object, Realm.create() returns undefined rather than the object itself.

realm.write(() => {
realm.create(WeatherSensor, {
_id: new BSON.objectId(),
deviceId: "WX1278UIT",
temperatureInFahrenheit: 66.7,
barometricPressureInHg: 29.65,
windSpeedInMph: 2,
});
});

You cannot read these objects. Once created, they sync to the App Services backend and the linked Atlas database.

Atlas Device Sync gestiona completamente el ciclo de vida de estos datos. Se mantiene en el dispositivo hasta que la sincronización de Data Ingest se completa y luego se elimina del dispositivo.

Volver

Set the Client Log Level

En esta página