11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Firestoreで条件を絞ってデータを削除する

Posted at

SQLではよくある、 DELETE FROM [テーブル名] WHERE [条件]; をFirestoreで実現する方法について書いてみようと思う。

そもそも DELETE FROM [テーブル名] WHERE [条件]; はFirestoreには用意されていない。

そもそも DELETE FROM [テーブル名] WHERE [条件]; はFirestoreでは実行できません。

なので、 whereで消したいデータを取得してそれを一件一件削除していくという実装 になる。

実装

実装方法は下記になる。今回はサンプルコードではTypeScriptを使っているが、適宜読み変えてください。


const query: FirebaseFirestore.QuerySnapshot = await db
    .collection("timeline")
    .where("uid", "==", followUserId)
    .get();

query.docs.forEach(async doc => {
    await doc.ref.delete();
});

以上です。

11
7
0

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
11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?