Luciano
Luciano

Reputation: 2798

FireBase Firestore retrieve multiple documents by list of ID's

I've got two collections for example Cities and Houses. Houses are not a nested collection of Cities, but a house contains a reference to a City.

Wat is the most efficient way to get city information for each house in a list?

For Example

class City {
  var name: String = ""
  var property1: String = ""
  var property2: String = ""
}

class House {
  var cityId: String = ""
  var propertyA: String = ""
  var propertyB: String = ""
}

I've got a list of houses, how do I get for each house in this list the name of the city? Is this possible by query, or should I just get all the cities (or each one seperate) and map them in code.

The cities and houses are just an example, but simular to my real situation.

Upvotes: 4

Views: 7678

Answers (4)

hamx0r
hamx0r

Reputation: 4278

For a web app (JS), this works: db.collection('myCollection').where(firebase.firestore.FieldPath.documentId(), "in", collectionIds).where('status', '==', 2) where collectionIds is an array of Document IDs.

Upvotes: 2

Alaa M.
Alaa M.

Reputation: 5273

This is how to get multiple documents by a list of IDs in Android.

//Get Firestore instance
FirebaseFirestore ref = FirebaseFirestore.getInstance();

List<String> ids;

//... Fill ids list with document ids

//Fire query
ref.collection("collection_name").whereIn(FieldPath.documentId(), ids).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

Upvotes: 1

You can use com.google.firebase.firestore.FieldPath. This is working for me in kotlin:

//friendsIDs is ArrayList<String>
    firestore!!.collection("users").whereIn(FieldPath.documentId(), friendsIDs).get().addOnCompleteListener {usersDocs ->
        if (usersDocs.isSuccessful) {
            val result = usersDocs.result!!
            val docs = result.documents
            for (userDoc in docs) {
                val friendData = userDoc.data!!
                //...
            }
        } else {
            Log.e("FirestoreRequest", "Error getting documents.", usersDocs.exception)
        }
    }

Upvotes: 3

Alex Mamo
Alex Mamo

Reputation: 138824

To simplest way that I can think of is to create a new property to each house object called cityName. So your database structure should look like this:

Firestore-root
   |
   --- houses (collection)
        |
        --- houseId (document)
              |
              --- propertyA: "Property A" (document propery)
              |
              --- propertyB: "Property B" (document propery)
              |
              --- cityName: "City Name"

how do i get for each house in this list the name of the city?

Just query your houses collection and get the corresponding city name. In code, should look like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference ref = rootRef.collection("Posts").document("Post");
ref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                String cityName = document.getString("cityName");
                Log.d("TAG", cityName);
            } else {
                Log.d("TAG", "No such document");
            }
        } else {
            Log.d("TAG", "get failed with ", task.getException());
        }
    }
});

So there is no need to keep a reference of the city in order to display the city name.

Upvotes: 1

Related Questions