LoginSignup
132
86

More than 3 years have passed since last update.

firestoreのreferenceとかsnapshotで混乱したので整理してみた

Last updated at Posted at 2019-12-13

firestoreを使ってて、あれコイツはdata()持ってないんだっけ、あれコイツにはupdate()できないんだっけ?と混乱したので整理してみました。

サマリ


// これはDocumentReference
// ただの参照であって実体はない
const shopDocRef = db.collection('shops').doc('1');

// add, set, update, deleteなどの操作は
// Referenceに対して行う
await shopDocRef.update({name: "レストラン品川"});

// referenceに対してget()で
// 実体であるDocumentSnapshotを得る
const shopDoc = await shopDocRef.get();

// DocumentSnapshotoにdata()すると
// データを取得できる
const shop = shopDoc.data();

Reference

referenceはただの参照。ドキュメントやコレクションへのパス情報だけで、中身を持っていない。

データの追加、更新、削除はreferenceに対して行う。

CollectionReference

コレクションへの参照。

// CollectionReference
// - id
// - path
// - parent
const shopsCollectionReference = db.collection('shops')

DocumentReference

ドキュメントへの参照。

// DocumentReference
// - id
// - path
// - parent
const shopDocumentReference = db
  .collection('shops') // ここまででCollcectionReferenceを取得
  .doc('1');

追加、更新、削除はreferenceに対して行う

特定のドキュメントの更新、削除

const shopDocumentReference = db
  .collection('shops')
  .doc('1');

// ドキュメント更新
await shopDocumentReference.update({name: "レストラン品川 1号店"})

// ドキュメント削除
await shopDocumentReference.delete()

コレクションにドキュメントを追加。下の例ではidを自動採番。

const shopsCollectionReference = db.collection('shops');

// ドキュメント追加
await shopsCollectionReference.add({name: "恵比寿コーヒー", place: "恵比寿"})

idを指定したい場合の書き方

// ドキュメントidに"4"を指定
const shopDocumentReference = db.collection("shops").doc("4");
// ドキュメント追加
await shopDocumentReference.set({ name: "恵比寿コーヒー", place: "恵比寿" });

Snapshot

中身の実態。
referenceに対してget()することで取得できる。
get()は非同期に実行されるのでawaitで実行する。

DocumentSnapshot

ドキュメントの実態。
.data()メソッドでデータのオブジェクトを取得できる。

// DocumentSnapshot
// - exist
// - id
// - ref
const shopDocumentSnapshot = await db
  .collection('shops')          // ここまででCollcectionReferenceを取得
  .doc('1') // ここまででDocumentReferenceを取得
  .get();                      // snapshotを非同期で取得

// data
const shop = shopDocumentSnapshot.data();

// DocumentReference
const shopDocumentReference = shopDocumentSnapshot.ref

Query関連

検索で使う。

Query

ただの検索条件の定義。まだ何も中身を取得していない。

// Query 
const query = db
  .collection('shops')
  .where('place', '==', '品川');

QuerySnapshot

Queryに対してget()すると検索を実行して中身を取得する。
その中身がQuerySnapshot

docsという配列を持っていて、この配列にqueryDocumentSnapshotが複数格納されている。
queryDocumentSnapshotDocumentSnapshotとほぼ同じぽい。

// QuerySnapshot
// - docs (Array)
// - empty (bool)
// - query (Query)
// - size (number)
const querySnapshot = await db
  .collection('shops')
  .where('place', '==', '品川')
  .get()

if(querySnapshot.size > 0){
  // DocumentSnapshotと同じ
  const queryDocumentSnapshot = querySnapshot.docs[0]
  // data
  const shop = queryDocumentSnapshot.data()
  // DocumentReference
  const bookDocumentReference =  queryDocumentSnapshot.ref 
}

参考URL

Cloud Firestore にデータを追加する

132
86
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
132
86