Search code examples
reactjsfirebasegoogle-cloud-firestorereact-redux-firebaseredux-firestore

react-redux-firebase - How to query documents from Firestore using an array of ID's?


I have an array of Document ID's. And I want to fetch these documents from firestore using their ID's from the array. In other words, I need to go through the array of ID's and query each document with the corresponding ID.

So at the moment this is how I'm querying. I'm querying all of the documents inside of the "Properties" collection.

export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  firestoreConnect((props) => {

      if (!props.auth.uid) {
        props.history.push('/signin')
        return []
      }

      return [


        {
          collection: 'Properties',

        },
        {
          collection: 'Properties',
          doc: propid
      },

        {
          collection: 'Partners',
          doc: props.auth.uid,
          subcollections: [{
              collection: 'MyInvites',

            },


          ]
        },
      {
        collection: 'Partners',
        doc: props.auth.uid,
        subcollections: [{
           collection: 'ReceivedInvites',
          }
        ]
      },
      {
        collection: 'Partners',
        doc: props.auth.uid,

      },

      ]


    }
  )

The goal is to get specific documents from the "Properties" collection by ID. And the ID's of the documents I want to query are in an array.

For example

let idArray=[doc0id,doc1id,doc2id,doc3id,....docnid]

Is there a way to achieve this?


Solution

  • What you're describing is an in query.

    So for anywhere up to 10 document IDs, you can do:

    myCollection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"])