Cloud Firestore Veri modeli

Cloud Firestore, NoSQL belge tabanlı bir veritabanıdır. SQL veritabanından farklı olarak tablo veya satır yoktur. Bunun yerine, verileri koleksiyonlar halinde düzenlenen dokümanlarda depolarsınız.

Her belge, bir anahtar/değer çifti grubu içerir. Cloud Firestore, küçük dokümanlardan oluşan büyük koleksiyonları depolamak için optimize edilmiştir.

Tüm dokümanlar koleksiyonlarda saklanmalıdır. Dokümanlar, hem dizeler gibi temel alanları hem de listeler gibi karmaşık nesneleri içerebilen alt koleksiyonlar ve iç içe yerleştirilmiş nesneler içerebilir.

Koleksiyonlar ve dokümanlar Cloud Firestore içinde örtülü olarak oluşturulur. Verileri bir koleksiyondaki dokümana atamanız yeterlidir. Koleksiyon veya doküman yoksa Cloud Firestore tarafından oluşturulur.

Dokümanlar

Cloud Firestore'da depolama birimi belgedir. Belge, değerlerle eşlenen alanlar içeren basit bir kayıttır. Her belge bir adla tanımlanır.

Bir kullanıcıyı temsil eden bir doküman alovelace şu şekilde görünebilir:

  • alovelace

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

Bir dokümandaki karmaşık, iç içe yerleştirilmiş nesnelere harita adı verilir. Örneğin, yukarıdaki örnekteki kullanıcı adını bir haritayla şu şekilde yapılandırabilirsiniz:

  • alovelace

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

Dokümanların JSON'a çok benzediğini fark edebilirsiniz. Aslında bu iki kavram temelde aynıdır. Bazı farklılıklar (ör. dokümanlar ek veri türlerini destekler ve boyutları 1 MB ile sınırlıdır) olsa da genel olarak dokümanları hafif JSON kayıtları olarak değerlendirebilirsiniz.

Koleksiyonlar

Dokümanlar, dokümanların bulunduğu kapsayıcılar olan koleksiyonlarda yer alır. Örneğin, çeşitli kullanıcılarınızı içeren bir users koleksiyonunuz olabilir. Bu koleksiyondaki her kullanıcı bir belgeyle temsil edilir:

  • kullanıcı

    • alovelace

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

    • aturing

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

Cloud Firestore şemasızdır. Bu nedenle, her belgeye hangi alanları yerleştireceğiniz ve bu alanlarda hangi veri türlerini saklayacağınız konusunda tamamen özgürsünüz. Aynı koleksiyondaki belgeler farklı alanlar içerebilir veya bu alanlarda farklı türlerde veriler depolayabilir. Ancak belgeleri daha kolay sorgulayabilmek için birden fazla belgede aynı alanları ve veri türlerini kullanmanız önerilir.

Koleksiyonlar yalnızca belge içerir. Doğrudan değer içeren ham alanlar ve diğer koleksiyonlar içeremez. (Cloud Firestore içinde daha karmaşık verilerin nasıl yapılandırılacağıyla ilgili açıklama için Hiyerarşik Veriler bölümüne bakın.)

Bir koleksiyondaki dokümanların adları benzersizdir. Kullanıcı kimlikleri gibi kendi anahtarlarınızı sağlayabilir veya Cloud Firestore'ın sizin için otomatik olarak rastgele kimlikler oluşturmasına izin verebilirsiniz.

Koleksiyon "oluşturmanız" veya "silmeniz" gerekmez. Bir koleksiyonda ilk belgeyi oluşturduktan sonra koleksiyon var olur. Bir koleksiyondaki tüm belgeleri silerseniz koleksiyon artık mevcut olmaz.

Referanslar

Cloud Firestore içindeki her doküman, veritabanındaki konumuyla benzersiz şekilde tanımlanır. Önceki örnekte, koleksiyon users içindeki bir doküman alovelace gösteriliyordu. Kodunuzda bu konuma başvurmak için konuma referans oluşturabilirsiniz.

Web

import { doc } from "firebase/firestore";

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

Web

var alovelaceDocumentRef = db.collection('users').doc('alovelace');
Swift
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
let alovelaceDocumentRef = db.collection("users").document("alovelace")
Objective-C
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
FIRDocumentReference *alovelaceDocumentRef =
    [[self.db collectionWithPath:@"users"] documentWithPath:@"alovelace"];

Kotlin

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 istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

C#

Cloud Firestore istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

Referans, yalnızca veritabanınızdaki bir konumu işaret eden basit bir nesnedir. Veri olup olmadığına bakılmaksızın referans oluşturabilirsiniz ve referans oluşturma işlemi herhangi bir ağ işlemi gerçekleştirmez.

Ayrıca koleksiyonlara referans da oluşturabilirsiniz:

Web

import { collection } from "firebase/firestore";

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

Web

var usersCollectionRef = db.collection('users');
Swift
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
let usersCollectionRef = db.collection("users")
Objective-C
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
FIRCollectionReference *usersCollectionRef = [self.db collectionWithPath:@"users"];

Kotlin

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 istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

C#

Cloud Firestore istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

Kolaylık sağlaması açısından, bir dokümanın veya koleksiyonun yolunu dize olarak belirterek de referans oluşturabilirsiniz. Yol bileşenleri eğik çizgiyle (/) ayrılır. Örneğin, alovelace dokümanına referans oluşturmak için:

Web

import { doc } from "firebase/firestore"; 

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

Web

var alovelaceDocumentRef = db.doc('users/alovelace');
Swift
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
let aLovelaceDocumentReference = db.document("users/alovelace")
Objective-C
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
FIRDocumentReference *aLovelaceDocumentReference =
    [self.db documentWithPath:@"users/alovelace"];

Kotlin

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 istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

C#

Cloud Firestore istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

Hiyerarşik Veriler

Hiyerarşik veri yapılarının Cloud Firestore'da nasıl çalıştığını anlamak için mesajlar ve sohbet odaları içeren bir örnek sohbet uygulamasını ele alalım.

Farklı sohbet odalarını saklamak için rooms adlı bir koleksiyon oluşturabilirsiniz:

  • oda

    • odaA

      name : "my chat room"

    • odaB

      ...

Artık sohbet odalarınız olduğuna göre, mesajlarınızı nasıl saklayacağınıza karar verin. Bu bilgileri sohbet odasının dokümanında saklamak istemeyebilirsiniz. Cloud Firestore içindeki dokümanlar hafif olmalıdır ve bir sohbet odası çok sayıda mesaj içerebilir. Ancak sohbet odanızın dokümanında alt koleksiyon olarak ek koleksiyonlar oluşturabilirsiniz.

Alt koleksiyonlar

Bu senaryoda iletileri depolamanın en iyi yolu alt koleksiyonları kullanmaktır. Alt koleksiyon, belirli bir dokümanla ilişkili bir koleksiyondur.

rooms koleksiyonunuzdaki her oda dokümanı için messages adlı bir alt koleksiyon oluşturabilirsiniz:

  • oda

    • odaA

      name : "my chat room"

      • mesajlar

        • message1

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

        • message2

          ...

    • odaB

      ...

Bu örnekte, aşağıdaki kodu kullanarak alt koleksiyondaki bir iletiye referans oluşturursunuz:

Web

import { doc } from "firebase/firestore"; 

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

Web

var messageRef = db.collection('rooms').doc('roomA')
                .collection('messages').doc('message1');
Swift
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
let messageRef = db
  .collection("rooms").document("roomA")
  .collection("messages").document("message1")
Objective-C
Not: Bu ürün, watchOS ve App Clip hedeflerinde kullanılamaz.
FIRDocumentReference *messageRef =
    [[[[self.db collectionWithPath:@"rooms"] documentWithPath:@"roomA"]
    collectionWithPath:@"messages"] documentWithPath:@"message1"];

Kotlin

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 istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

$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 istemcisini yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları başlıklı makaleyi inceleyin.

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

Koleksiyonlar ve dokümanların dönüşümlü olarak sıralandığını fark edin. Koleksiyonlarınız ve dokümanlarınız her zaman bu kalıba uymalıdır. Bir koleksiyondaki koleksiyona veya bir dokümandaki dokümana referans veremezsiniz.

Alt koleksiyonlar, verileri hiyerarşik olarak yapılandırmanıza olanak tanır. Böylece verilere erişmek kolaylaşır. roomA içindeki tüm mesajları almak için messages alt koleksiyonuna bir koleksiyon referansı oluşturabilir ve bu referansla diğer koleksiyon referanslarıyla etkileşimde bulunduğunuz gibi etkileşimde bulunabilirsiniz.

Alt koleksiyonlardaki dokümanlar da alt koleksiyonlar içerebilir. Bu sayede verileri daha fazla iç içe yerleştirebilirsiniz. Verileri 100 düzeye kadar iç içe yerleştirebilirsiniz.