Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 603 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Firestore: Query by item in array of document

#1
I have 2 collections **"photos"** and **"users"** and each document in **"users"** has **one or more photo IDs with an array**.

```
photos > 5528c46b > name: "Photo1"
a1e820eb > name: "Photo2"
32d410a7 > name: "Photo3"
```

```
users > acd02b1d > name: "John", photos: ["5528c46b"]
67f60ad3 > name: "Tom", photos: ["5528c46b", "32d410a7"]
7332ec75 > name: "Sara", photos: ["a1e820eb"]
9f4edcc1 > name: "Anna", photos: ["32d410a7"]
```

I want to get **all users who have one or more specific photo IDs**.

Are there any ways to do that?
Reply

#2
Here is a bit of expansion on the answer as some seem to be confused about having to make indexes for each key, Firestore already indexes your data for simple queries thus you can do a simple query like

documentReference.where('param','==','value').onSnapshot(...)

but you can not do a compound query unless you index your data for those parameters. So you would need indexes to be able to do something like this:

documentReference.where('param','==','value').where(..otherparams...).onSnapshot(...)

So as long as you need the photos for an id you can save them as

usersCollection : (a collection)
uidA: (a document)
photoField: (a field value that is a map or object)
fieldID1 : true (a property of the photoField)
fieldID2 : true (a property of the photoField)
etc ...


and you can simply query user(s) that have, let's say, fieldID1 in their photoField without needing to form any index and like query below.

firestore.doc('usersCollection/uidA').where('photoField.fieldID1','==',true).onSnapshot(...)

Reply

#3
> Added 'array-contains' query operator for use with .where() to find documents where an array field contains a specific element.

[To see links please register here]

5.3.0

**Update**: also available in `@google-cloud/firestore`:

[To see links please register here]


**Update 2**

[To see links please register here]


**Update 3** now available in Admin Node.js SDK v6.0.0

[To see links please register here]

Reply

#4
> See [Henry's answer][1], as we've no made Array Contains queries available.

Unfortunately not yet, although it's on our roadmap.

In the meantime, you'll need to use a map instead, in the form of:

photos: {
id1: true
id2: true
}

Now you can find all users with id1 by filtering by `photos.id1 == true`.

Read more about querying such sets in the [Firebase documentation](

[To see links please register here]

).


[1]:

[To see links please register here]

Reply

#5
Firestore has now added an 'in' query as of November 2019.
According to [the announcement article][1]:

> With the in query, you can query a specific field for multiple values
> (up to 10) in a single query. You do this by passing a list containing
> all the values you want to search for, and Cloud Firestore will match
> any document whose field equals one of those values.


[1]:

[To see links please register here]

Reply

#6
<h2>With Firebase Version 9 (Dec, 2021 Update):</h2>

You can use **"array-contains"** with **one single photo document ID** in the **"while()"** to get **all users who have it**:

```js
import {
query,
collection,
where,
getDocs
} from "firebase/firestore";

// Here
const q = query(
collection(db, "users"),
where("photos", "array-contains", "5528c46b")
);
// Here

const usersDocsSnap = await getDocs(q);

usersDocsSnap .forEach((doc) => {
console.log(doc.data()); // "John's doc", "Tom's doc"
});
```

You can alse use **"array-contains-any"** with **one or more photo document IDs with an array** in the **"while()"** to get **more corresponding users**:

```js
import {
query,
collection,
where,
getDocs
} from "firebase/firestore";

// Here
const q = query(
collection(db, "users"),
where("photos", "array-contains-any", ["5528c46b", "a1e820eb"])
);
// Here

const usersDocsSnap = await getDocs(q);

usersDocsSnap .forEach((doc) => {
console.log(doc.data()); // "John's doc", "Tom's doc", "Sara's doc"
});
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through