Firestore Search for Values in Multiple Arrays












0















I have two arrays in each of my Firestore documents. In my app you can search based on values in those arrays. I am able to get my search working if I look for exact matches between my arrays that get created based on what options are selected and look for exact matches in my documents like this.



let _ = circleQuery.observe(.documentEntered, with: { (key, location) in
if let key = key {
Constants.firestore.collection("facilities").whereField("facility_type", isEqualTo: self.type!)
.whereField("insurance_accepted", isEqualTo: self.insurance!).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
self.addFaciltiesToMap()
}
}
}
}
}
})


The problem with this is that that this isn't looking at individual values. I want to display results based on whether to not a value from my local array is contained in the two document arrays. The user has options that can be selected from each of those arrays and they don't have to select from both so they can select options that are only contained in one or the other. For example, they can select between 0 and 4 options from the "facility_type" array and between 0 and 2 options from the "insurance_accepted" array. So, if they only select options from "facility_type", I only want to look for the selected values in that document array and the same goes for the "insurance_accepted" document array. If they select options from both, I need to look for options in both. If they are only selecting from one or the other, I need to return results based on whether their selected values exists in that one array in the document. If they select both I need to return results based on whether to not at least one match is made in each document array.



This is basically what I'm trying to achieve.



for value in self.type! {
Constants.firestore.collection("facilities").whereField("facility_type", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}

for value in self.insurance! {
Constants.firestore.collection("facilities").whereField("insurance_accepted", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}









share|improve this question

























  • It sounds like you need to read about arrayContains filters. firebase.google.com/docs/firestore/query-data/…

    – Doug Stevenson
    Jan 2 at 19:28











  • My issue is that only one arrayContains filter can be used per query so I'm not entirely sure how to go about this when I need to look in two arrays.

    – raginggoat
    Jan 2 at 19:30











  • You can search two arrays by having multiple where clauses. The documentation for that is on the same page I linked. Are you saying that's not working?

    – Doug Stevenson
    Jan 2 at 19:38











  • This is what I was referring to. "you can include at most one array_contains clause in a compound query"

    – raginggoat
    Jan 2 at 19:40






  • 1





    Instead of lists, you will need to use an object where the values of the lists are instead fields of the objects, and they all just map to 'true'.

    – Doug Stevenson
    Jan 2 at 20:47
















0















I have two arrays in each of my Firestore documents. In my app you can search based on values in those arrays. I am able to get my search working if I look for exact matches between my arrays that get created based on what options are selected and look for exact matches in my documents like this.



let _ = circleQuery.observe(.documentEntered, with: { (key, location) in
if let key = key {
Constants.firestore.collection("facilities").whereField("facility_type", isEqualTo: self.type!)
.whereField("insurance_accepted", isEqualTo: self.insurance!).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
self.addFaciltiesToMap()
}
}
}
}
}
})


The problem with this is that that this isn't looking at individual values. I want to display results based on whether to not a value from my local array is contained in the two document arrays. The user has options that can be selected from each of those arrays and they don't have to select from both so they can select options that are only contained in one or the other. For example, they can select between 0 and 4 options from the "facility_type" array and between 0 and 2 options from the "insurance_accepted" array. So, if they only select options from "facility_type", I only want to look for the selected values in that document array and the same goes for the "insurance_accepted" document array. If they select options from both, I need to look for options in both. If they are only selecting from one or the other, I need to return results based on whether their selected values exists in that one array in the document. If they select both I need to return results based on whether to not at least one match is made in each document array.



This is basically what I'm trying to achieve.



for value in self.type! {
Constants.firestore.collection("facilities").whereField("facility_type", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}

for value in self.insurance! {
Constants.firestore.collection("facilities").whereField("insurance_accepted", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}









share|improve this question

























  • It sounds like you need to read about arrayContains filters. firebase.google.com/docs/firestore/query-data/…

    – Doug Stevenson
    Jan 2 at 19:28











  • My issue is that only one arrayContains filter can be used per query so I'm not entirely sure how to go about this when I need to look in two arrays.

    – raginggoat
    Jan 2 at 19:30











  • You can search two arrays by having multiple where clauses. The documentation for that is on the same page I linked. Are you saying that's not working?

    – Doug Stevenson
    Jan 2 at 19:38











  • This is what I was referring to. "you can include at most one array_contains clause in a compound query"

    – raginggoat
    Jan 2 at 19:40






  • 1





    Instead of lists, you will need to use an object where the values of the lists are instead fields of the objects, and they all just map to 'true'.

    – Doug Stevenson
    Jan 2 at 20:47














0












0








0








I have two arrays in each of my Firestore documents. In my app you can search based on values in those arrays. I am able to get my search working if I look for exact matches between my arrays that get created based on what options are selected and look for exact matches in my documents like this.



let _ = circleQuery.observe(.documentEntered, with: { (key, location) in
if let key = key {
Constants.firestore.collection("facilities").whereField("facility_type", isEqualTo: self.type!)
.whereField("insurance_accepted", isEqualTo: self.insurance!).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
self.addFaciltiesToMap()
}
}
}
}
}
})


The problem with this is that that this isn't looking at individual values. I want to display results based on whether to not a value from my local array is contained in the two document arrays. The user has options that can be selected from each of those arrays and they don't have to select from both so they can select options that are only contained in one or the other. For example, they can select between 0 and 4 options from the "facility_type" array and between 0 and 2 options from the "insurance_accepted" array. So, if they only select options from "facility_type", I only want to look for the selected values in that document array and the same goes for the "insurance_accepted" document array. If they select options from both, I need to look for options in both. If they are only selecting from one or the other, I need to return results based on whether their selected values exists in that one array in the document. If they select both I need to return results based on whether to not at least one match is made in each document array.



This is basically what I'm trying to achieve.



for value in self.type! {
Constants.firestore.collection("facilities").whereField("facility_type", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}

for value in self.insurance! {
Constants.firestore.collection("facilities").whereField("insurance_accepted", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}









share|improve this question
















I have two arrays in each of my Firestore documents. In my app you can search based on values in those arrays. I am able to get my search working if I look for exact matches between my arrays that get created based on what options are selected and look for exact matches in my documents like this.



let _ = circleQuery.observe(.documentEntered, with: { (key, location) in
if let key = key {
Constants.firestore.collection("facilities").whereField("facility_type", isEqualTo: self.type!)
.whereField("insurance_accepted", isEqualTo: self.insurance!).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
self.addFaciltiesToMap()
}
}
}
}
}
})


The problem with this is that that this isn't looking at individual values. I want to display results based on whether to not a value from my local array is contained in the two document arrays. The user has options that can be selected from each of those arrays and they don't have to select from both so they can select options that are only contained in one or the other. For example, they can select between 0 and 4 options from the "facility_type" array and between 0 and 2 options from the "insurance_accepted" array. So, if they only select options from "facility_type", I only want to look for the selected values in that document array and the same goes for the "insurance_accepted" document array. If they select options from both, I need to look for options in both. If they are only selecting from one or the other, I need to return results based on whether their selected values exists in that one array in the document. If they select both I need to return results based on whether to not at least one match is made in each document array.



This is basically what I'm trying to achieve.



for value in self.type! {
Constants.firestore.collection("facilities").whereField("facility_type", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}

for value in self.insurance! {
Constants.firestore.collection("facilities").whereField("insurance_accepted", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}






ios arrays swift google-cloud-firestore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 20:15







raginggoat

















asked Jan 2 at 19:25









raginggoatraginggoat

1,68863379




1,68863379













  • It sounds like you need to read about arrayContains filters. firebase.google.com/docs/firestore/query-data/…

    – Doug Stevenson
    Jan 2 at 19:28











  • My issue is that only one arrayContains filter can be used per query so I'm not entirely sure how to go about this when I need to look in two arrays.

    – raginggoat
    Jan 2 at 19:30











  • You can search two arrays by having multiple where clauses. The documentation for that is on the same page I linked. Are you saying that's not working?

    – Doug Stevenson
    Jan 2 at 19:38











  • This is what I was referring to. "you can include at most one array_contains clause in a compound query"

    – raginggoat
    Jan 2 at 19:40






  • 1





    Instead of lists, you will need to use an object where the values of the lists are instead fields of the objects, and they all just map to 'true'.

    – Doug Stevenson
    Jan 2 at 20:47



















  • It sounds like you need to read about arrayContains filters. firebase.google.com/docs/firestore/query-data/…

    – Doug Stevenson
    Jan 2 at 19:28











  • My issue is that only one arrayContains filter can be used per query so I'm not entirely sure how to go about this when I need to look in two arrays.

    – raginggoat
    Jan 2 at 19:30











  • You can search two arrays by having multiple where clauses. The documentation for that is on the same page I linked. Are you saying that's not working?

    – Doug Stevenson
    Jan 2 at 19:38











  • This is what I was referring to. "you can include at most one array_contains clause in a compound query"

    – raginggoat
    Jan 2 at 19:40






  • 1





    Instead of lists, you will need to use an object where the values of the lists are instead fields of the objects, and they all just map to 'true'.

    – Doug Stevenson
    Jan 2 at 20:47

















It sounds like you need to read about arrayContains filters. firebase.google.com/docs/firestore/query-data/…

– Doug Stevenson
Jan 2 at 19:28





It sounds like you need to read about arrayContains filters. firebase.google.com/docs/firestore/query-data/…

– Doug Stevenson
Jan 2 at 19:28













My issue is that only one arrayContains filter can be used per query so I'm not entirely sure how to go about this when I need to look in two arrays.

– raginggoat
Jan 2 at 19:30





My issue is that only one arrayContains filter can be used per query so I'm not entirely sure how to go about this when I need to look in two arrays.

– raginggoat
Jan 2 at 19:30













You can search two arrays by having multiple where clauses. The documentation for that is on the same page I linked. Are you saying that's not working?

– Doug Stevenson
Jan 2 at 19:38





You can search two arrays by having multiple where clauses. The documentation for that is on the same page I linked. Are you saying that's not working?

– Doug Stevenson
Jan 2 at 19:38













This is what I was referring to. "you can include at most one array_contains clause in a compound query"

– raginggoat
Jan 2 at 19:40





This is what I was referring to. "you can include at most one array_contains clause in a compound query"

– raginggoat
Jan 2 at 19:40




1




1





Instead of lists, you will need to use an object where the values of the lists are instead fields of the objects, and they all just map to 'true'.

– Doug Stevenson
Jan 2 at 20:47





Instead of lists, you will need to use an object where the values of the lists are instead fields of the objects, and they all just map to 'true'.

– Doug Stevenson
Jan 2 at 20:47












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012071%2ffirestore-search-for-values-in-multiple-arrays%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012071%2ffirestore-search-for-values-in-multiple-arrays%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas