Cloud Firestore Veri modeli

Cloud Firestore, NoSQL tabanlı, belge odaklı 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 doküman bir dizi anahtar/değer çifti 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 depolanmalıdır. Dokümanlar, alt koleksiyonlar ve iç içe yerleştirilmiş nesneler içerebilir. Bunların her ikisi de dize gibi temel alanlar veya liste gibi karmaşık nesneler içerebilir.

Koleksiyonlar ve dokümanlar Cloud Firestore içinde dolaylı olarak oluşturulur. Verileri bir koleksiyondaki dokümana atamanız yeterlidir. Koleksiyon veya doküman yoksa Cloud Firestore bunları oluşturur.

Dokümanlar

Cloud Firestore'da depolama birimi dokümandır. Belge, değerlerle eşlenen alanlar içeren hafif bir kayıttır. Her doküman bir adla tanımlanır.

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 denir. Örneğin, yukarıdaki örnekte yer alan 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, temelde aynıdırlar. Bazı farklılıklar vardır (örneğin, dokümanlar ek veri türlerini destekler ve doküman boyutu sınırına tabidir) ancak genel olarak dokümanları basit JSON kayıtları olarak değerlendirebilirsiniz.

Koleksiyonlar

Belgeler, belgeler için kapsayıcı 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

    • aturing

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

Cloud Firestore şemasızdır. Bu nedenle, her dokümana hangi alanları ekleyeceğiniz ve bu alanlarda hangi veri türlerini depolayacağınız konusunda tamamen özgürsünüz. Aynı koleksiyondaki belgelerin tümü farklı alanlar içerebilir veya bu alanlarda farklı veri türleri depolayabilir. Ancak dokümanları daha kolay sorgulayabilmek için birden fazla dokümanda aynı alanları ve veri türlerini kullanmak iyi bir fikirdir.

Koleksiyonlar yalnızca doküman içerir. Doğrudan değer içeren ham alanlar ve başka 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.)

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

Koleksiyon "oluşturmanız" veya "silmeniz" gerekmez. Koleksiyondaki ilk dokümanı oluşturduktan sonra koleksiyon oluşturulur. Bir koleksiyondaki tüm dokümanları silerseniz koleksiyon da silinir.

Referanslar

Cloud Firestore içindeki her doküman, veritabanındaki konumuyla benzersiz şekilde tanımlanır. Önceki örnekte, users koleksiyonundaki alovelace dokümanı gösterilmişti. Kodunuzda bu konuma başvurmak 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

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ı 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 istemcisi 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, veritabanınızdaki bir konumu işaret eden basit bir nesnedir. Veri olup olmamasından bağımsız olarak bir referans oluşturabilirsiniz. Referans oluşturmak herhangi bir ağ işlemi gerçekleştirmez.

Koleksiyonlara da referans 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 istemcisi 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 istemcisi 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ğlamak için 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 istemcisi 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 istemcisi 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

Cloud Firestore uygulamasında hiyerarşik veri yapılarının işleyiş şeklini anlamak için mesajlar ve sohbet odaları içeren örnek bir sohbet uygulamasını ele alalım.

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

  • oda

    • odaA

      name : "my chat room"

    • roomB

      ...

Sohbet odalarınız olduğuna göre mesajlarınızı nasıl saklayacağınıza karar verin. Bunları sohbet odasının dokümanında saklamak istemeyebilirsiniz. Cloud Firestore içindeki dokümanlar hafif 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 mesajları 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"

      • mesaj

        • message1

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

        • message2

          ...

    • roomB

      ...

Bu örnekte, aşağıdaki kodla 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

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ı 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 istemcisi 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ın ve belgelerin dönüşümlü olarak sıralandığına dikkat 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ıyarak verilere erişimi kolaylaştırır. roomA içindeki tüm mesajları almak için messages alt koleksiyonuna bir koleksiyon referansı oluşturabilir ve diğer koleksiyon referanslarında olduğu gibi bu referansla 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.