Cloud Firestore データモデル

Cloud Firestore は NoSQL ドキュメント指向データベースです。SQL データベースとは違い、テーブルや行はありません。代わりに、データは「ドキュメント」に格納し、それが「コレクション」にまとめられます。

各「ドキュメント」には、一連の Key-Value ペアが含まれています。Cloud Firestore は、小さなドキュメントからなる大きなコレクションを格納するために最適化されています。

ドキュメントはすべてコレクションに保存されなければなりません。ドキュメントには、「サブコレクション」と、ネストされたオブジェクトを格納できます。このどちらにも、文字列などの基本フィールドや、リストなどの複雑なオブジェクトを含めることができます。

コレクションとドキュメントは Cloud Firestore で暗黙的に作成されます。ユーザーはデータをコレクション内のドキュメントに割り当てるだけです。コレクションまたはドキュメントのいずれかが存在しない場合は、Cloud Firestore によって作成されます。

ドキュメント

Cloud Firestore では、ストレージの単位はドキュメントになります。ドキュメントは、値にマッピングされるフィールドを含む軽量のレコードです。各ドキュメントは名前で識別されます。

ユーザー alovelace を表すドキュメントは次のようになります。

  • alovelace

    first : "Ada"
    last : "Lovelace"
    born : 1815

ドキュメント内のネストされた複雑なオブジェクトはマップと呼ばれます。たとえば、上記の例のユーザー名をマップで構造化すると、次のようになります。

  • alovelace

    name :
        first : "Ada"
        last : "Lovelace"
    born : 1815

見てわかるように、ドキュメントは JSON によく似ており、実際基本的には JSON と同じです。いくつかの違いはありますが(たとえばドキュメントでは追加のデータ型がサポートされており、サイズは 1 MB までに制限されています)、一般的にドキュメントは軽量の JSON レコードとして扱うことができます。

コレクション

ドキュメントはコレクションの中にあります。コレクションは端的に言えばドキュメントのコンテナです。たとえば、users コレクションを作成して、さまざまなユーザーを表すドキュメントを格納できます。

  • users

    • alovelace

      first : "Ada"
      last : "Lovelace"
      born : 1815

    • aturing

      first : "Alan"
      last : "Turing"
      born : 1912

Cloud Firestore はスキーマレスなので、各ドキュメントにどのフィールドを入れるかやそのフィールドにどのデータ型を格納するかを、完全に自由に決めることができます。同じコレクション内のドキュメントすべてに異なるフィールドを含めたり、それらのフィールドに異なるデータ型を格納したりできます。ただし、複数のドキュメントで同じフィールドとデータ型を使用するほうが、ドキュメントのクエリが容易になります。

コレクションにはドキュメントだけが含まれます。コレクションに値を持つ生のフィールドをコレクションに直接含めることはできず、他のコレクションを含めることもできません。Cloud Firestore でより複雑なデータを構造化する方法については、階層データをご覧ください。

コレクション内のドキュメントの名前は一意です。ユーザー ID などの独自のキーを指定することも、Cloud Firestore で自動的にランダムな ID を作成することもできます。

コレクションを特に「作成」したり、「削除」したりする必要はありません。コレクション内の最初のドキュメントを作成すると、コレクションが暗黙的に作成され、コレクション内のドキュメントをすべて削除すると、コレクションも削除されます。

リファレンス

Cloud Firestore のドキュメントはすべて、データベース内の場所によって一意に識別されます。前の例では、コレクション users 内のドキュメント alovelace を示しました。コード内でこの場所を参照するには、その場所への「リファレンス」を作成します。

ウェブ向けのモジュラー API

import { doc } from "firebase/firestore";

const alovelaceDocumentRef = doc(db, 'users', 'alovelace');

ウェブ向けの名前空間方式 API

var alovelaceDocumentRef = db.collection('users').doc('alovelace');
Swift
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Objective-C
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
FIRDocumentReference *alovelaceDocumentRef =
    [[self.db collectionWithPath:@"users"] documentWithPath:@"alovelace"];

Kotlin+KTX

val alovelaceDocumentRef = db.collection("users").document("alovelace")

Java

DocumentReference alovelaceDocumentRef = db.collection("users").document("alovelace");

Dart

final alovelaceDocumentRef = db.collection("users").doc("alovelace");
Java
// Reference to a document with id "alovelace" in the collection "users"
DocumentReference document = db.collection("users").document("alovelace");
Python
a_lovelace_ref = db.collection("users").document("alovelace")

Python

a_lovelace_ref = db.collection("users").document("alovelace")
C++
DocumentReference alovelace_document_reference =
    db->Collection("users").Document("alovelace");
Node.js
const alovelaceDocumentRef = db.collection('users').doc('alovelace');
Go

import (
	"cloud.google.com/go/firestore"
)

func createDocReference(client *firestore.Client) {

	alovelaceRef := client.Collection("users").Doc("alovelace")

	_ = alovelaceRef
}
PHP

PHP

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

$document = $db->collection('samples/php/users')->document('alovelace');
Unity
DocumentReference documentRef = db.Collection("users").Document("alovelace");
C#

C#

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

DocumentReference documentRef = db.Collection("users").Document("alovelace");
Ruby
document_ref = firestore.col("users").doc("alovelace")

リファレンスは、単にデータベース内の場所を示す軽量なオブジェクトです。リファレンスはそこにデータがあるかどうかにかかわらず作成でき、リファレンスを作成してもネットワーク操作は実行されません。

コレクションへのリファレンスも作成できます。

ウェブ向けのモジュラー API

import { collection } from "firebase/firestore";

const usersCollectionRef = collection(db, 'users');

ウェブ向けの名前空間方式 API

var usersCollectionRef = db.collection('users');
Swift
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
let usersCollectionRef = db.collection("users")
Objective-C
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
FIRCollectionReference *usersCollectionRef = [self.db collectionWithPath:@"users"];

Kotlin+KTX

val usersCollectionRef = db.collection("users")

Java

CollectionReference usersCollectionRef = db.collection("users");

Dart

final usersCollectionRef = db.collection("users");
Java
// Reference to the collection "users"
CollectionReference collection = db.collection("users");
Python
users_ref = db.collection("users")

Python

users_ref = db.collection("users")
C++
CollectionReference users_collection_reference = db->Collection("users");
Node.js
const usersCollectionRef = db.collection('users');
Go

import (
	"cloud.google.com/go/firestore"
)

func createCollectionReference(client *firestore.Client) {
	usersRef := client.Collection("users")

	_ = usersRef
}
PHP

PHP

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

$collection = $db->collection('samples/php/users');
Unity
CollectionReference collectionRef = db.Collection("users");
C#

C#

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

CollectionReference collectionRef = db.Collection("users");
Ruby
collection_ref = firestore.col "users"

便宜上、ドキュメントまたはコレクションへのパスを文字列として指定し、パス コンポーネントをスラッシュ(/)で区切ってリファレンスを作成することもできます。たとえば、alovelace ドキュメントへのリファレンスは次のように作成します。

ウェブ向けのモジュラー API

import { doc } from "firebase/firestore"; 

const alovelaceDocumentRef = doc(db, 'users/alovelace');

ウェブ向けの名前空間方式 API

var alovelaceDocumentRef = db.doc('users/alovelace');
Swift
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
let aLovelaceDocumentReference = db.document("users/alovelace")
Objective-C
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
FIRDocumentReference *aLovelaceDocumentReference =
    [self.db documentWithPath:@"users/alovelace"];

Kotlin+KTX

val alovelaceDocumentRef = db.document("users/alovelace")

Java

DocumentReference alovelaceDocumentRef = db.document("users/alovelace");

Dart

final aLovelaceDocRef = db.doc("users/alovelace");
Java
// Reference to a document with id "alovelace" in the collection "users"
DocumentReference document = db.document("users/alovelace");
Python
a_lovelace_ref = db.document("users/alovelace")

Python

a_lovelace_ref = db.document("users/alovelace")
C++
DocumentReference alovelace_document = db->Document("users/alovelace");
Node.js
const alovelaceDocumentRef = db.doc('users/alovelace');
Go

import (
	"cloud.google.com/go/firestore"
)

func createDocReferenceFromString(client *firestore.Client) {
	// Reference to a document with id "alovelace" in the collection "users"
	alovelaceRef := client.Doc("users/alovelace")

	_ = alovelaceRef
}
PHP

PHP

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

$document = $db->document('users/alovelace');
Unity
DocumentReference documentRef = db.Document("users/alovelace");
C#

C#

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

DocumentReference documentRef = db.Document("users/alovelace");
Ruby
document_path_ref = firestore.doc "users/alovelace"

階層データ

Cloud Firestore で階層データ構造がどのように機能するかを理解するために、メッセージとチャットルームを使ったチャットアプリの例を見てみましょう。

さまざまなチャットルームを格納するための rooms と呼ばれるコレクションを作成できます。

  • rooms

    • roomA

      name : "my chat room"

    • roomB

      ...

これでチャットルームができたので、メッセージを保存する方法を指定します。チャットルームのドキュメントに保存することはおすすめしません。Cloud Firestore 内のドキュメントは軽量にする必要がありますが、チャットルームには膨大な数のメッセージが格納される可能性があるからです。ただし、チャットルームのドキュメント内にサブコレクションとして追加のコレクションを作成できます。

サブコレクション

このシナリオでメッセージを保存するための最善の方法は、サブコレクションを使用することです。サブコレクションは特定のドキュメントに関連付けられたコレクションです。

rooms コレクション内のすべてのルーム ドキュメントに、messages というサブコレクションを作成できます。

  • rooms

    • roomA

      name : "my chat room"

      • messages

        • message1

          from : "alex"
          msg : "Hello World!"

        • message2

          ...

    • roomB

      ...

この例では、次のコードを使用して、サブコレクション内のメッセージへのリファレンスを作成します。

ウェブ向けのモジュラー API

import { doc } from "firebase/firestore"; 

const messageRef = doc(db, "rooms", "roomA", "messages", "message1");

ウェブ向けの名前空間方式 API

var messageRef = db.collection('rooms').doc('roomA')
                .collection('messages').doc('message1');
Swift
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
let messageRef = db
  .collection("rooms").document("roomA")
  .collection("messages").document("message1")
Objective-C
注: このプロダクトは、watchOS と App Clip の各ターゲットでは利用できません。
FIRDocumentReference *messageRef =
    [[[[self.db collectionWithPath:@"rooms"] documentWithPath:@"roomA"]
    collectionWithPath:@"messages"] documentWithPath:@"message1"];

Kotlin+KTX

val messageRef = db
    .collection("rooms").document("roomA")
    .collection("messages").document("message1")

Java

DocumentReference messageRef = db
        .collection("rooms").document("roomA")
        .collection("messages").document("message1");

Dart

final messageRef = db
    .collection("rooms")
    .doc("roomA")
    .collection("messages")
    .doc("message1");
Java
// Reference to a document in subcollection "messages"
DocumentReference document =
    db.collection("rooms").document("roomA").collection("messages").document("message1");
Python
room_a_ref = db.collection("rooms").document("roomA")
message_ref = room_a_ref.collection("messages").document("message1")

Python

room_a_ref = db.collection("rooms").document("roomA")
message_ref = room_a_ref.collection("messages").document("message1")
C++
DocumentReference message_reference = db->Collection("rooms")
    .Document("roomA")
    .Collection("messages")
    .Document("message1");
Node.js
const messageRef = db.collection('rooms').doc('roomA')
  .collection('messages').doc('message1');
Go

import (
	"cloud.google.com/go/firestore"
)

func createSubcollectionReference(client *firestore.Client) {
	messageRef := client.Collection("rooms").Doc("roomA").
		Collection("messages").Doc("message1")

	_ = messageRef
}
PHP

PHP

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

$document = $db
    ->collection('rooms')
    ->document('roomA')
    ->collection('messages')
    ->document('message1');
Unity
DocumentReference documentRef = db
	.Collection("Rooms").Document("RoomA")
	.Collection("Messages").Document("Message1");
C#

C#

Cloud Firestore クライアントのインストールと作成の詳細については、Cloud Firestore クライアント ライブラリをご覧ください。

DocumentReference documentRef = db
    .Collection("Rooms").Document("RoomA")
    .Collection("Messages").Document("Message1");
Ruby
message_ref = firestore.col("rooms").doc("roomA").col("messages").doc("message1")

コレクションとドキュメントが交互になるよう注意してください。コレクションとドキュメントは常にこのパターンで従う必要があります。コレクション内のコレクションや、ドキュメント内のドキュメントは参照できません。

サブコレクションを使用すると、データを階層的に構造化できるため、データへのアクセスが容易になります。roomA にあるすべてのメッセージを取得するために、サブコレクション messages へのコレクション リファレンスを作成して、それを他のコレクション リファレンスと同じように操作できます。

サブコレクション内のドキュメントにもサブコレクションを格納できるため、データをさらにネストできます。データは最大 100 レベルまでネストできます。