Cloud Firestore Veri modeli

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

Her doküman bir dizi anahtar/değer çifti içerir. Cloud Firestore, büyük boyutlu küçük doküman koleksiyonlarını depolamak için optimize edilmiştir.

Tüm dokümanlar koleksiyonlarda depolanmalıdır. Belgeler, alt koleksiyonlar ve iç içe yerleştirilmiş nesneler içerebilir. Bunların her ikisi de dizeler gibi temel alanları veya listeler gibi karmaşık nesneleri içerebilir.

Koleksiyonlar ve belgeler, Cloud Firestore'da dolaylı olarak oluşturulur. Bir koleksiyondaki dokümana veri atamanız yeterlidir. Koleksiyon veya belge 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 belgenin bir adı bulunur.

alovelace kullanıcısını temsil eden bir doküman aşağıdaki gibi 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 örnekte verilen 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. Öyle diyebiliriz. Bazı farklılıklar vardır (örneğin, dokümanlar ek veri türlerini destekler ve boyut olarak 1 MB ile sınırlıdır) ancak genel olarak dokümanları basit JSON kayıtları olarak değerlendirebilirsiniz.

Koleksiyonlar

Dokümanlar, yalnızca doküman kapsayıcıları olan koleksiyonlarda bulunur. Örneğin, her biri bir belgeyle temsil edilen, çeşitli kullanıcılarınızı içeren bir users koleksiyonunuz olabilir:

  • kullanıcı

    • alovelace

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

    • turu

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

Cloud Firestore şemasız olduğundan her belgeye yerleştireceğiniz alanlar ve bu alanlarda hangi veri türlerini depolayacağınız konusunda tam özgürlük vardır. Aynı koleksiyondaki belgelerin tümü farklı alanlar içerebilir veya bu alanlarda farklı veri türleri depolayabilir. Ancak, belgeleri daha kolay sorgulayabilmeniz için birden fazla belgede aynı alanları ve veri türlerini kullanmak iyi bir fikirdir.

Koleksiyonlar yalnızca belgeler içerir ancak başka hiçbir öğe içermez. Değer içeren ham alanları doğrudan içeremez ve başka koleksiyonlar içeremez. (Cloud Firestore'da daha karmaşık verilerin nasıl yapılandırılacağıyla ilgili açıklama için Hiyerarşik Veriler'e bakın.)

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

Koleksiyonları "oluşturmanız" veya "silmeniz" gerekmez. Koleksiyondaki ilk belgeyi oluşturduğunuzda koleksiyon oluşturulur. Bir koleksiyondaki tüm dokümanları silerseniz koleksiyon artık mevcut olmaz.

Referanslar

Cloud Firestore'daki her belge, veritabanındaki konumuyla benzersiz şekilde tanımlanır. Önceki örnekte, users koleksiyonundaki alovelace adlı bir doküman gösteriliyordu. Kodunuzda bu konuma referans vermek için bir 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+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 istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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

C#

Cloud Firestore istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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

Referans, yalnızca veritabanınızdaki bir konuma işaret eden hafif bir nesnedir. Burada veri olup olmamasına bakılmaksızın bir referans oluşturabilirsiniz. Referans oluşturmak, herhangi bir ağ işlemi gerçekleştirmez.

Ayrıca koleksiyonlar için referanslar 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+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 istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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

C#

Cloud Firestore istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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 bir dize olarak belirterek de referans oluşturabilirsiniz. Yol bileşenleri düz 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+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 istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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

C#

Cloud Firestore istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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 örnek bir sohbet uygulaması kullanmayı düşünün.

Farklı sohbet odalarını depolamak için rooms adında bir koleksiyon oluşturabilirsiniz:

  • oda

    • oda A

      name : "my chat room"

    • oda B

      ...

Artık sohbet odalarınız olduğuna göre mesajlarınızı nasıl depolayacağınıza karar verebilirsiniz. Bunları sohbet odasının dokümanında saklamak istemeyebilirsiniz. Cloud Firestore'daki belgeler basit olmalıdır ve bir sohbet odasında çok sayıda mesaj bulunabilir. Ancak sohbet odanızın dokümanında alt koleksiyonlar 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şkilendirilmiş koleksiyonlardır.

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

  • oda

    • oda A

      name : "my chat room"

      • mesaj

        • mesaj1

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

        • mesaj2

          ...

    • oda B

      ...

Bu örnekte, aşağıdaki kodu kullanarak alt koleksiyondaki bir mesaja 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+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 istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

$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 istemcisi yükleme ve oluşturma hakkında daha fazla bilgi için Cloud Firestore İstemci Kitaplıkları'na bakın.

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

Koleksiyonların ve dokümanların alternatif kalıbına dikkat edin. Koleksiyonlarınız ve dokümanlarınız her zaman bu kalıbı izlemelidir. Koleksiyondaki bir koleksiyona veya dokümandaki bir dokümana referans veremezsiniz.

Alt koleksiyonlar, verileri hiyerarşik olarak yapılandırmanıza olanak tanıyarak verilere erişimi kolaylaştırır. roomA uygulamasındaki tüm mesajları almak üzere messages alt koleksiyona yönelik bir koleksiyon referansı oluşturabilir ve diğer koleksiyon referanslarında olduğu gibi bu alt koleksiyonla etkileşimde bulunabilirsiniz.

Alt koleksiyonlardaki belgeler, alt koleksiyonlar da içerebilir. Bu sayede verileri daha fazla iç içe yerleştirebilirsiniz. Verileri 100 düzey derine kadar iç içe yerleştirebilirsiniz.