Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

firebase - Firestore how to query field containing any of the values in the array

Say my Firestore DB contains a collection of documents, each with a field that contains a large array of numbers. For Example:

{
arr: [11,13,24,16,37,50]
},
{
arr: [12,34,55,56]
},
{
arr: [12,16,27,59]
}

How can I make a query that returns all the documents where the 'arr' field contains any of the values in a certain array?

For example, if I query with [16,13] I get the first and third documents only (first one contains both 16 and 13, third one contains 16).

Please note that both the 'arr' array and the array in my query can contain a large number of values (> 1000), so I can't use 'array-contains-any'.

Is it possible to do that? Can I structure my DB differently in order to achieve that goal?

question from:https://stackoverflow.com/questions/65838086/firestore-how-to-query-field-containing-any-of-the-values-in-the-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As you know, array-contains-any support up to 10 comparison values. If you have a large array of over 1000 values, then you might be better to restructure your collection to something like this:

Example:

arr:
  arr_name:
    title: "arr_title"
        arr_1: {
            "10": true,
            "11": true,
            "12": true
        }

Example:

db.collection("arr_name")
    .whereField("arr_1.10", isEqualTo: true)
    .whereField("arr_1.12", isEqualTo: true)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...