LoginSignup
0
0

More than 1 year has passed since last update.

Firestoreのコレクション内全てのドキュメントから特定のフィールドを削除する方法

Posted at

結論から申し上げると

実装例

  • usersコレクションの全ドキュメントからnameフィールドを削除する例(Javascript)
  • Firebaseへ接続するためのセットアップは省略
const firebase = require("firebase/app");
require("firebase/firestore");

const targetFieldName = 'name';
const delField = {[targetFieldName] : firebase.firestore.FieldValue.delete()};

(async () => {  
  const colRef = firebase.firestore().collection('users');
  colSnapshot.forEach((doc) => {
    colRef.doc(doc.id).update(delField);
  });
})();

補足

  • 実行時間は15,000件で10秒かからない程度だった。非常に早い。
  • 高速で実行されるが故、途中で止めることができないので、readline-syncなどを使ってコンソール上で実行前の最終確認を入れておくとチームに喜ばれるだろう
  • Firebaseコンソール上で該当のコレクションを開いておくと、削除の過程を視認できるので、作業中は開いておくことをお勧めする

参考資料

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