Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

カスタムユーザーデータ - C++ SDK

項目一覧

  • ユーザーのカスタム データの読み取り
  • ユーザーのカスタム データ ドキュメントの作成
  • ユーザーのカスタム データの更新
  • ユーザーのカスタム データの削除

現在ログインしているユーザーのカスタム ユーザー データは、そのユーザーの Userオブジェクトから読み取ることができます。 Userオブジェクトを使用してカスタム ユーザー データを編集することはできません。 カスタム ユーザー データを編集するには、「カスタム ユーザー データの更新」を参照してください。 データを読み取るには、ログイン ユーザーのUserオブジェクトのcustom_dataプロパティにアクセスします。

// Custom user data could be stale, so refresh it before reading it
user.refresh_custom_user_data().get();
auto userData = user.custom_data().value();
/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto userDataObject = nlohmann::json::parse(userData);
CHECK(userDataObject["favoriteColor"] == "gold");

警告

カスタム データは古い可能性があります

Atlas App Services は、基礎となるデータが変更されたときにすぐに、クライアント側のユーザー カスタム データ ドキュメントの値を動的に更新しません。 代わりに、ユーザーがアクセス トークンを更新するたびに、App Services は最新バージョンのカスタム ユーザー データを取得します。このトークンは App Services バックエンドにアクセスするほとんどの SDK 操作で使用されます。 トークンがデフォルトの30分の有効期限前に更新されない場合、C++ SDK はバックエンドに対する次回の呼び出しでトークンを更新します。 カスタム ユーザー データは、最大30分、バックエンドへの次の SDK 呼び出しが発生するまでの時間古くなる可能性があります。

注意

最新バージョンのカスタム ユーザー データが必要な場合は、 refresh_custom_user_data()関数を使用して、ユーザーのカスタム データの最新バージョンをリクエストします。

ユーザーのカスタム ユーザー データを作成するには、カスタム ユーザー データ コレクションに MongoDB ドキュメントを作成します。 ドキュメントの ユーザー ID フィールドには、ユーザーのユーザー ID が含まれている必要があります。

Tip

Atlas App Services UI で、 Custom User Dataタブの下の App Usersページを確認して、次のようなカスタム ユーザー データ設定を検索して構成します。

  • カスタムユーザーデータクラスター、データベース、コレクション

  • カスタムユーザーデータドキュメントをユーザーにマッピングするために使用されるユーザーID フィールド

このドキュメントを作成する方法の 1 つは、カスタム ユーザー データ コレクションにカスタム データ ドキュメントを挿入するAtlas Functionを呼び出すことです。 Atlas Function からカスタム ユーザー データを追加するための単一パターンはありません。 アプリケーションのユースケースに合わせて関数を記述する必要があります。

この例では、Atlas Function はクライアントによって渡されたオブジェクトを受け取り、Atlas のカスタムユーザーデータコレクションに追加されます。 関数は、カスタム ユーザー データがまだ存在しない場合はそれを作成し、存在する場合はその中のすべてのデータを置き換えます。

updateCustomUserData.js - サーバー上で実行中のAtlas Function ( JavaScript )
exports = async function updateCustomUserData(newCustomUserData) {
const userId = context.user.id;
const customUserDataCollection = context.services
.get("mongodb-atlas")
.db("custom-user-data-database")
.collection("cpp-custom-user-data");
const filter = { userId };
// Replace the existing custom user data document with the new one.
const update = { $set: newCustomUserData };
// Insert document if it doesn't already exist
const options = { upsert: true };
const res = await customUserDataCollection.updateOne(filter, update, options);
return res;
};

次の例では、現在ログインしているユーザーのユーザー ID とfavoriteColor値を含むドキュメントをカスタム ユーザー データ コレクションに挿入する関数を呼び出します。

auto user = app.login(realm::App::credentials::anonymous()).get();
// Functions take a string argument. Any quotes within the array must be
// escaped.
auto customData =
"[{\"userId\":\"" + user.identifier() + "\",\"favoriteColor\":\"gold\"}]";
// Call an Atlas Function to insert custom data for the user
auto result = user.call_function("updateCustomUserData", customData).get();

カスタム ユーザー データ ドキュメントの作成時に、任意の数のフィールドと値を追加できます。 ユーザー ID フィールドは、ドキュメントがカスタム ユーザー データとしてUserオブジェクトで使用可能になるための 唯一 の要件です。

Atlas FunctionMongoDB Compass 、またはMongoDB Atlas Data Explorer を使用して、カスタム ユーザー データをアップデートできます。

To update a user's custom user data with an Atlas Function, edit the MongoDB document whose user ID field contains the user ID of the user. 次の例では、上記のカスタム ユーザー データ ドキュメントの作成に使用されるのと同じ関数を呼び出します。 ここでは、現在ログインしているユーザーのユーザー ID を含む ドキュメントのfavoriteColorフィールドをアップデートします。

// Functions take a string argument. Any quotes within the array must be
// escaped.
auto updatedData = "[{\"userId\":\"" + user.identifier() +
"\",\"favoriteColor\":\"black\"}]";
// Call an Atlas Function to update custom data for the user
auto updateResult =
user.call_function("updateCustomUserData", updatedData).get();
// Refresh the custom user data before reading it to verify it succeeded
user.refresh_custom_user_data().get();
auto updatedUserData = user.custom_data().value();
/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto updatedUserDataObject = nlohmann::json::parse(updatedUserData);
CHECK(updatedUserDataObject["favoriteColor"] == "black");

Tip

ユーザーの ID を確認するには、 App Users user.identifier()プロパティにアクセスするか、App Services UI で [] ページの [ Users ] タブでユーザーを見つけます。

カスタム ユーザー データは、ユーザー オブジェクトにリンクされたドキュメントに保存されます。 ユーザーを削除しても、カスタム ユーザー データは削除されません。 例えとして Apple のアカウント削除ガイダンス に従うためにユーザー データを完全に削除するには、 の場合、ユーザーのカスタム データ ドキュメントを手動で削除する必要があります。

Atlas FunctionMongoDB Compass 、またはMongoDB Atlas Data Explorer を使用してカスタム ユーザー データを削除できます。

この例では、Atlas Function は引数を必要としません。 この関数は、 関数コンテキスト を使用して呼び出し元のユーザー ID を判断し、ユーザーの ID と一致するカスタム ユーザー データ ドキュメントを削除します。

deleteCustomUserData.js - サーバー上で実行中の Atlas Function(JavaScript)
exports = async function deleteCustomUserData() {
const userId = context.user.id;
const customUserDataCollection = context.services
.get("mongodb-atlas")
.db("custom-user-data-database")
.collection("cpp-custom-user-data");
const filter = { userId };
const res = await customUserDataCollection.deleteOne(filter);
return res;
};

この関数を呼び出すコードでは、関数を呼び出すにはログインしたユーザーのみが必要です。

auto deleteResult = user.call_function("deleteCustomUserData", "[]").get();

戻る

メール/パスワードユーザーの管理