0

Let's say I have a collection named pet with the following structure for a given document

document_ID = {
    cat: either the cat's name in string or null
    dog: either the dog's name in string or null
}

I'd like to query all the documents based on the following condition: either 'cat' or 'dog' field has non-null value. That is, exclude only the documents where both 'cat' and 'dog' fields are null.

If it were 'and' condition, I'd run the query like below, but how do I do the query based on 'or' condition? Is this even possible? If not, what might be a possible workaround?

firestore.collection('pet').where('cat','!=',null).where('dog','!=',null).get()
.then(querysnapshot=>{
    //Do some operations with the result
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
J.Ko
  • 6,561
  • 3
  • 12
  • 28

1 Answers1

1

I think it's not even possible to do firestore queries with || operator. But you can simply do 2 separate queries and then merge data if you need.

firestore.collection('pet').where('dog','!=',null).get()
.then(querysnapshot=>{
    //Do some operations with the result
})

firestore.collection('pet').where('cat','!=',null).get()
.then(querysnapshot=>{
    //Do some operations with the result
})
pasty
  • 380
  • 2
  • 8