Skip to content

FR: Firestore OR operator in WHERE query #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EpicButterz opened this issue Nov 16, 2017 · 153 comments
Closed

FR: Firestore OR operator in WHERE query #321

EpicButterz opened this issue Nov 16, 2017 · 153 comments

Comments

@EpicButterz
Copy link

EpicButterz commented Nov 16, 2017

[REQUIRED] Describe your environment

  • Operating System version: N/A
  • Firebase SDK version: 4.6.2
  • Firebase Product: firestore

[REQUIRED] Describe the problem

Rather than having to make several separate queries I would like to see an OR operator for the WHERE query in Firestore. This is something that is currently available in MongoDB or with the $or operator here.

Relevant Code:

The query could look something like this:

citiesRef.where("state", "==", "CA", "||", "state", "==", "AZ")

or better yet

citiesRef.where("state == CA || state == AZ")
@EpicButterz
Copy link
Author

EpicButterz commented Nov 16, 2017

For my specific case something more like an in operator would be more appropriate. I am looking to retrieve documents from a collection that correspond to a list of IDs. So something like:

citiesRef.in('id', ['P4ZoMnrNRQjnMGmuMmvc', 'VSw2KyP1YaBRnkheq5kB', 'i1658JL5a2Itv2l0BC8B'])

@agersoncgps
Copy link

OR and IN / CONTAINS / LIKE are pretty basic concepts in query languages. Confused why they are missing here.

@mikelehen
Copy link
Contributor

Thanks for the feedback! These query improvements (OR, IN / CONTAINS, and LIKE queries) are all on our radar for future Cloud Firestore query improvements. I'm closing this issue as it's not specific to the JS SDK, but we know that these features would be very useful and there's a lot of demand for them. We intend to address them in the future as we continue to evolve Firestore's query functionality. Thanks!

@dzoba
Copy link

dzoba commented Jan 23, 2018

I would be very interested in these as well.

@andreluisce
Copy link

Any news about it?

@taishikato
Copy link

I am looking forward to this feature

@atcults
Copy link

atcults commented Feb 13, 2018

Any news on ORing two filters ?

@csoreff
Copy link

csoreff commented Feb 14, 2018

I'd kill for a NOT IN [] >.<

@katothompson
Copy link

This would be a great feature to have!

@schassande
Copy link

I am looking forward to this feature too !

@jkampitakis
Copy link

Same here, I am looking forward to this feature.

@cnolimit
Copy link

cnolimit commented Mar 9, 2018

can't wait to see the LIKE query addition

@mdrideout
Copy link

mdrideout commented Mar 11, 2018

For anyone struggling for a solution, I mirrored the parts of firestore database relevant to search in elasticsearch. It's a search engine for no-sql database data, and can do WAY more than a "LIKE" or "WHERE" clause in a sql query. It's very customizable. Any additional data needed from the search results can then be pulled from firestore. In my app, elasticsearch handles any search queries, and data is pulled from firestore.

You can setup a stack on GCP relatively easily and for free, and now elasticsearch has their own stack deployment option (which can also use GCP, and provides more security options).
https://www.elastic.co/
https://bitnami.com/stack/elk

@githubbla
Copy link

I am looking forward to this feature too !

@rtman
Copy link

rtman commented Apr 2, 2018

second all this.. OR is very necessary. Trying to concat lists after two WHERE queries is not great.

@1001daysofcode
Copy link

Can't wait for this feature!

@m-a-zhou
Copy link

m-a-zhou commented Apr 8, 2018

This feature would be very helpful.

@CyrusZei
Copy link

any ETA on this ?

@mikelehen
Copy link
Contributor

We can't provide ETAs on upcoming features, but as I said before, OR queries and IN queries are definitely on our radar and we appreciate all the interest.

For what it's worth, IN queries are likely more straightforward and so we may tackle them first, something along the lines of what @EpicButterz described in #321 (comment). I'd be curious to hear how many of you would find that useful or if you're looking for more general-purpose OR queries. And if you're looking for more general-purpose OR queries, some details on your use cases (e.g. examples of specific queries that you'd want to do in your app) could help us design the feature. Thanks!

@csoreff
Copy link

csoreff commented Apr 12, 2018

@mikelehen IN and NOT IN are more useful I think. OR would be nice to have but for now we can just combine the results of multiple queries for that.

@mikelehen
Copy link
Contributor

Thanks. I'm curious if there's a strong desire for "NOT IN" (against a list of values) or if just "!=" (against a single value) would be sufficient for the use cases you have in mind.

@csoreff
Copy link

csoreff commented Apr 12, 2018

@mikelehen Let's say I have a list of keys for ignored items. I'd like to be able to easily grab all items where the key is NOT IN []. != is certainly desired as well, but they have different use cases. Right now I'm forced to grab all items and then manually filter out the ignored items on client side.

@antonybudianto
Copy link

antonybudianto commented Apr 12, 2018

i want a datastore like this:

members: [
  {
    id: 123
  },
  {
    id: 231
  }
],

teams: [
  {
    id: 1,
    members: [123, 231]
  },
  {
    id: 2,
    members: [123]
  }
]

but because there's no IN query, I've to duplicate the relationship:

members: [
  {
    id: 123,
    teams: [1, 2]
  },
  {
    id: 231,
    teams: [1]
  }
],

/* teams remain the same **/

my requirement is that:
a user must be able to know his/her teams
and a team must know its members.

@mikelehen
Copy link
Contributor

@antonybudianto Thanks for the feedback. If I'm understanding correctly, I'd actually characterize this as a slightly different feature and the good news is that:

  1. You can solve this today by modeling your teams as { id: 1, members: { "123": true, "231": true } } and then for user 123 to find all their teams you can do a query like teamsCollection.where("members.123", "==", true).
  2. We're looking into adding a new feature that would let you query based on array values which would let you keep your desired schema (with arrays) and do something like teamsCollection.where("members", "array_contains", 123). I can't promise any timelines but this may launch before we get to OR / IN queries.

@antonybudianto
Copy link

antonybudianto commented Apr 12, 2018

Thanks for the solution. But one more, suppose I visit the team "A" page, and want to display its members there (I want to display their names/other fields, not ids), how do you query it (userCollections should contains no relationship object because teamcollections already have it)?

@mikelehen
Copy link
Contributor

@antonybudianto Ahh, yeah. To get a member's name, etc. you'd need to look that up from the members collection presumably. Cloud Firestore doesn't support JOIN operations and that's unlikely to change in the foreseeable future. That said, if we added an IN query as described in #321 (comment) then you could look up all the members with a single additional query once you have their IDs.

Thanks for the feedback.

@antonybudianto
Copy link

@mikelehen yes, IN operation will be sufficient for my case, thanks for the consideration.

@CyanDeveloper
Copy link

+1

@davidtlee
Copy link

@mikelehen What I really need is the "IN" case. The "OR" case is manageable just by doing two queries and handling it on the frontend. But "IN" is really needed for eager loading. If I am querying a collection and for each of those collections, I need to do separate queries (e.g. to get authors for each post), that opens up tons of connections. Having an "IN" query would enable that to be done with just two queries.

@timscullin
Copy link

+1 so essential

@long1eu
Copy link

long1eu commented Feb 23, 2019 via email

@efraespada
Copy link

+1 essential

@permadiberlianto
Copy link

+1, we need it

@CyrusZei
Copy link

CyrusZei commented Mar 7, 2019

could people please stop commenting on this and just add a Reaction instead?!?!

if you want this feature just add a 👍 on this comment and stop adding useless comments

@jeremyfrancis
Copy link

YES! Let's commit to terrorizing Firebase with our request for these features! Everyone like this post with a THUMBS UP and contact firebase directly!!!

@sam26880
Copy link

sam26880 commented Mar 7, 2019

+1

@devpato
Copy link

devpato commented Mar 10, 2019

The fact that this issue has 135 comments plus all the reactions and Firestore team hasn't done anything about it's alarming.

@Kinark
Copy link

Kinark commented Mar 11, 2019

@devpato True. +1 anyway

@thirajade
Copy link

this feature is truely needed

@awtmarriott
Copy link

come on, just copy the MONGO "Find" syntax...

@devpato
Copy link

devpato commented Mar 21, 2019

Decided to open the exact same issue since they closed this one and haven't seen any reply from firebase latetly #1624

@rockwotj
Copy link
Contributor

rockwotj commented Mar 21, 2019

Hey all,

Backend features should be filed at Cloud Firestore's public issue trackers.

I took the liberty of filing two features based on this GitHub issue:

It'd be great if you could hit the star button if you're interested in one or both features. Additionally if you have a specific usecase that you think would be useful for Google to keep in mind when building this feature please put it there. You can also CC yourself there to get updates from the team - as @mikelehen already has pointed out, this GitHub issue tracker is for the JS SDK, and this feature needs support in the backend. I'd encourage you to file other new feature requests that are backend specific in that issue tracker too!

@davidtlee
Copy link

davidtlee commented Mar 21, 2019 via email

@rockwotj
Copy link
Contributor

rockwotj commented Mar 21, 2019

Screen Shot 2019-03-21 at 5 28 20 PM

Sorry you actually have to hit the little star icon

@mesqueeb
Copy link

I humbly request all 81 participants of this thread (+158 persons who upvoted) to ⭐️ these issues, so that they can finally start looking into this!

@jeremyfrancis
Copy link

Hey all,

Backend features should be filed at Cloud Firestore's public issue trackers.

I took the liberty of filing two features based on this GitHub issue:

It'd be great if you could hit the star button if you're interested in one or both features. Additionally if you have a specific usecase that you think would be useful for Google to keep in mind when building this feature please put it there. You can also CC yourself there to get updates from the team - as @mikelehen already has pointed out, this GitHub issue tracker is for the JS SDK, and this feature needs support in the backend. I'd encourage you to file other new feature requests that are backend specific in that issue tracker too!

Just starred it. Everyone take a minute to do the same please!

@darren-dev
Copy link

Anyone reading this, please go to the following links and click on the STAR icon at the left of the title:
https://issuetracker.google.com/issues/129070817
https://issuetracker.google.com/issues/129056739

image

@artolshansky
Copy link

artolshansky commented Mar 29, 2019

+1 (sub for news and in the issue tracker too)

@jamesreilly5
Copy link

Thank you for raising the issue @darren-dev, starred and commented!

@jamesreilly5
Copy link

Have raised another separate issue for NOT IN clause here - https://issuetracker.google.com/issues/129805446. Please star if this is blocking you too

@darren-dev
Copy link

darren-dev commented Apr 5, 2019

@jamesreilly5 I may have a possible idea of a draft-fix, if you're open to entertaining the idea while your issue is there...

Basically, store the datePosted as a UNIX timestamp on each document, then on the user's end, each time they go to that page, update a field on the user such as dateSeen with the current UNIX timestamp.

Then you can easily do a where on the collection to only get the posts after a date (emulating only nre posts you haven't seen) such as

.collection('/posts').where('datePosted', '>=', user.dateSeen)

It's not perfect, but it's something :)

@jamesreilly5
Copy link

@darren-dev yes ideally this would work but my number of posts per day is not that many so the newer posts would be quickly exhausted. My total number of posts if high (enough) though so if I can keep them random enough it'll work. I'll either take the hit of querying again when duplicates are fetched or think about how I can better distribute my randomness (currently a value between 1 and 1,000,000). I have some ideas in mind. Thanks for the suggestion

@mpivchev
Copy link

mpivchev commented May 7, 2019

Firestore devs: "Let's pretend this request doesn't exist"

@wilhuff
Copy link
Contributor

wilhuff commented May 7, 2019

We're quite aware that this request exists, and are actively working on making this happen. Unfortunately, nearly all the work to support this on the backend where it's invisible.

We'll update here when we have something to share.

@firebase firebase locked and limited conversation to collaborators May 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests