How to query documents which contains a sent parameter in Firestore?

前端 未结 1 721
遥遥无期
遥遥无期 2021-01-12 12:41

I am using the new Cloud Firestore in Android. So I am facing this issue in firestore. I want to query a document, which contains an id in their array fields.

for ex

1条回答
  •  囚心锁ツ
    2021-01-12 13:18

    This is a common question, so we have posted some documentation on how to work with arrays in Cloud Firestore: https://firebase.google.com/docs/firestore/solutions/arrays

    In short, if you want to query for array membership it's better to store your data as a map of booleans.

    So instead of this:

    {
      name: "sam",
      userGgIds: [12345, 67890]
    }
    

    You want this:

    {
      name: "sam",
      userGgIds: {
        12345: true,
        67890: true
      }
    }
    

    Then you can do this query to search for documents that contain the ID "12345"

    db.collection("users").where("userGgIds.12345", "==", true).get()
    

    0 讨论(0)
提交回复
热议问题