简体   繁体   中英

Get values from collection Cloud Firestore based on a filtering query with several .where

在此处输入图像描述

From the collection "matches" filtered by "league_id" if equal to 468 then if matches array in any position jornada array contains "Fri May 01 2020".

I've tried this but no joy. I belive that, If there is a way to combine the 2nd and 3th where in one will do the trick....

let query = await db.firestore().collection("matches");

query = await query.where("league_id", "==", "468");
console.log(query);

query.where("matches", "array-contains", "jornada"); //2nd
query
  .where("jornada", "array-contains", today)//3th
  .onSnapshot(function(querySnapshot) {
    console.log(querySnapshot);

    var cities = [];
    querySnapshot.forEach(function(doc) {

      console.log(doc.data());


    });
  });

The first query that you perform:

query = await query.where("league_id", "==", "468");

Will work perfectly fine because you have a league_id property that holds the 468 value, however, the addition of the following call:

query.where("matches", "array-contains", "jornada");

It will not work because you are telling Firestore to return all documents that contain in the matches array the jornada String. This is not possible because in the matches array, you are not storing String values. The matches is actually an array that holds objects, objects which in terms contain some properties of type array. Such kinds of queries actually are not supported by Firestore.

What can you instead, is to create another array property within your document that will actually hold String values. Add in that array the exact String values on which you want to filter the data and the second query will work too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM