0
0

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でdocumentがあってもcollecction.sizeがずっと0だった原因

Posted at

こんにちわ、CROSSER開発者です:relaxed:
今回は初歩的(?)なミスで3時間悩まされた(検索しても記事もなく。。。)ことについて
みなさんに共有していきたいと思います:fist:
どなたかの参考になれば嬉しいです:sparkles:

事象

この様なDB構造で、documentにフィールドがない時にいくらcollection getしても
collectionのsizeが0でした。
collection/document/collection/

例えばこの様な構造です。
user/userId/userDetail/
userIdドキュメントにはフィールドなし

この時にCollection('user').get()をしてもドキュメントが複数あってもフィールドがないので、get()されるドュメントの数は0になってしまいます。

解決方法

フィールドを追加する。

ただ、単純にフィールドは追加できません。
なぜなら、Collection('user').get()で取得できるドキュメントがないからです。

今回、私たちはドキュメントIDを別のCollectionに保存していたため、
そのIDで直接ドキュメントを指定し、フィールドを追加していきました。

例)
set(userCollection.doc(userId),{セットしたいフィールド})

まとめ

  • documentがあってもフィールドがなければCollectionのSizeが0になる

運用・保守の観点からもフィールドが不要なdocumentでも何かしらフィールドをつけていきたいと思います。
もしかしたら、公式サイトに書かれているかもしれませんが見つけきれなかったです:sob:

その他

語学マッチングアプリ『CROSSER』もよろしくお願いします。:two_hearts:
評価もいただけると嬉しいです!
iOS/Androidモバイル対応
https://instabio.cc/crosser

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?