Cloud Firestore Veri modeli

Cloud Firestore, NoSQL, belge tabanlı bir veritabanıdır. SQL veritabanının aksine tablo veya satır yok. Bunun yerine, verilerinizi sizin için saklanmış olan dokümanlarda koleksiyonlar halinde düzenlenir.

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

Tüm dokümanlar koleksiyonlarda depolanmalıdır. Dokümanlar şunları içerebilir: alt koleksiyonlar ve iç içe yerleştirilmiş nesneler (her ikisi de temel alanlar içerebilir) dizeler veya listeler gibi karmaşık nesneler gibi.

Koleksiyonlar ve belgeler, Cloud Firestore'da dolaylı olarak oluşturulur. Bir koleksiyondaki dokümana veri atamanız yeterlidir. Koleksiyon veya belge mevcut değilse Cloud Firestore belgeyi oluşturur.

Dokümanlar

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

alovelace kullanıcısını temsil eden bir doküman aşağıdaki gibi görünebilir:

  • alovelace

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

ziyaret edin.

Bir dokümandaki karmaşık, iç içe yerleştirilmiş nesnelere harita adı verilir. Örneğin herkesin Kullanıcı adını yukarıdaki örnekten yola çıkarak bir haritayla şu şekilde yapılandırın:

  • 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 dosya boyutu 1 MB ile sınırlıdır), ancak genel olarak dokümanları basit JSON kayıtları.

Koleksiyonlar

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

  • kullanıcı

    • alovelace

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

    • turu

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

Cloud Firestore şemasız olduğundan alanları ve bu alanlarda hangi veri türlerini depoladığınızı gösterir. Aynı koleksiyondaki dokümanların tümü farklı alanlar veya depo içerebilir veri türlerini konuşacağız. Ancak bunun için birden çok belgede aynı alanları ve veri türlerini kapsar. Böylece, kolaylaştırmaktadır.

Koleksiyonlar yalnızca belgeler içerir ancak başka hiçbir öğe içermez. Doğrudan RAW içeremez alanları değer içerir ve diğer koleksiyonları içeremez. (Bkz. Hiyerarşik Veriler'i tıklayın. verileri için de geçerlidir.)

Bir koleksiyondaki dokümanların adları benzersizdir. Kendi URL'lerinizi sağlayabilirsiniz anahtarları (kullanıcı kimlikleri gibi) kullanabilir veya Cloud Firestore'un rastgele kimlikler oluşturmasına izin verebilirsiniz. sizin için otomatik olarak.

"Oluşturmanız" gerekmez veya "sil" koleksiyonlar oluşturun. İlk dosyayı oluşturduktan sonra doküman bir koleksiyonda bulunuyorsa koleksiyon var demektir. Tüm doküman koleksiyonda bulunuyorsa bu koleksiyon artık mevcut değildir.

Referanslar

Cloud Firestore'daki her belge, konumuyla benzersiz şekilde tanımlanır söz konusu olabilir. Önceki örnekte, içinde alovelace başlıklı bir doküman gösterildi users koleksiyonu. Kodunuzda bu konuma referans vermek için, referansta bulunabilir.

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ı.

$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ı.

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

Referans, yalnızca projenizdeki bir konuma işaret eden hafif bir nesnedir Burada veri olup olmamasına bakılmaksızın bir referans oluşturabilir ve referans oluşturulması 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ı.

$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ı.

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

Kolaylık sağlaması açısından, bir yol bileşenleri ileriye doğru ayrılmış şekilde, dize olarak doküman veya koleksiyon eğik çizgi (/). Ö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ı.

$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ı.

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ı 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. B bunları sohbet odasının dokümanında depolamak istiyorsunuz. Cloud Firestore'daki belgeler basit olmalıdır ve bir sohbet odası çok sayıda mesaj içerebilir. Ancak, sohbet odanızın dokümanında ek koleksiyonlar oluşturabilirsiniz. bunu her zaman alt koleksiyonlar halinde yapabilirsiniz.

Alt koleksiyonlar

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

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

  • oda

    • oda A

      name : "my chat room"

      • . mesaj

        • . mesaj1

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

        • . mesaj2

          ...

    • oda B

      ...

Bu örnekte, alt koleksiyondaki bir mesaja referans aşağıdaki kodla değiştirin:

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ı.

$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ı.

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 her zaman bu kalıbı izlemelidir. Bir koleksiyona referans veremezsiniz kolayca erişilebilir hale getirebilirsiniz.

Alt koleksiyonlar, verileri hiyerarşik olarak yapılandırmanıza olanak tanır. Böylece, erişim. roomA uygulamasındaki tüm mesajları almak için koleksiyon referansı oluşturabilirsiniz ve diğer alt koleksiyona benzer şekilde bu alt koleksiyonla messages koleksiyonu referansını gösterir.

Alt koleksiyonlardaki dokümanlar alt koleksiyonlar da içerebilir. daha fazla iç içe yerleştirme. Verileri 100 düzey derine kadar iç içe yerleştirebilirsiniz.