Ssubrat Rrudra
Ssubrat Rrudra

Reputation: 960

Querying Firestore documents by Array or Map fields values in Firebase console

Here, I want to query by the value "ministoreid1" in Firebase console. But I am not able to figure out. Here, I am trying to do the query in console not using codes.

Upvotes: 3

Views: 2146

Answers (4)

nativelectronic
nativelectronic

Reputation: 862

###FILTER BLOGS BY USER.

for example if you have two collections (one to many)

/users
/blogs

blog and user has these schemes:

blog: { name,date,user:{myusername:true}}

//notice that user is a map or object and document blog has id itself wich you can use in user document and viceversa.

user:{name,lastname,blogs:{idblog1:true,idblog2:true}} //blogs is a map or object

if you want to filter by map object you can do this:

import firebase from "firebase/compat/app";
import { getFirestore } from "firebase/firestore";

const appFirebase = firebase.initializeApp(firebaseConfig);
export const dbFirebase = getFirestore(appFirebase);

const myuser= "myusername"
const q = query(collection(dbFirebase, "blogs"), where(`user.${myuser}`, "==", true));
const blogsSnapshot = await getDocs(q);

blogsSnapshot.forEach((doc) => {

      // doc.data() is never undefined for query doc snapshots
      console.log(doc.id, " => ", doc.data());
    });

console.log({blogsSnapshot}); 

Upvotes: 0

Asquare17
Asquare17

Reputation: 426

For future use, Firebase has added the feature request by Ssuburat. You can now can filter your documents in the Firebase console according to a value that exist in an array.Image here

Upvotes: 1

Ssubrat Rrudra
Ssubrat Rrudra

Reputation: 960

I have filed the feature request at Alex' suggestion. And the reply I received from Firebase Support:

Currently, there is no query similar to array-contains available in the Firestore Console. I can file a feature request ticket on your behalf. However, I can't guarantee anything at the moment, so please watch out for any updates on our blog or release notes for now. For the map field, you can try to filter on the console using the format: 'mapFieldName.keyName' in the field text box

So we can query for map values by 'mapFieldName.keyName'. I didn't know this before.

Upvotes: 4

Alex Mamo
Alex Mamo

Reputation: 138989

Here, I am trying to do the query in console not using codes.

Unfortunately, there is currently no way you can filter your documents in the Firebase console according to a value that exist in an array. As you can see, there are only the following operators:

== is equal to

> is after

>= is after and includes

< is before

<= is before and includes

But an whereArrayContains option it is not present yet. I recommend you file a feature request for that. It might be also useful for other developers.

The query that you perform in the console does't return any results because you are checking if the mini_stores_assigned is equal to ministoreid1, which obviously is not since the mini_stores_assigned property is an array and not a String so you can compare them.

Upvotes: 2

Related Questions