Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore - OR query

How to get data by multiple values of one field? For example, I have database with posts and I want to query for all posts where blogId is 1 or 2, sorting by timestamp.

collection("posts").whereEqualTo("blogId", "1") .whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50) 

Code above is not working :(

How to achieve this? Regards :)

like image 820
Skye Avatar asked Oct 13 '17 09:10

Skye


People also ask

What is Firebase and firestore?

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing: Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model.

Is Firebase firestore SQL or NoSQL?

Firebase is a NoSQL database; MySQL, as the name suggests, is a SQL database.

What to Use Firestore for?

Cloud Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline. When the device comes back online, Cloud Firestore synchronizes any local changes back to Cloud Firestore.

Is firestore better than Firebase?

What are the differences between Firebase and Firestore? Firebase is a more compressive solution vs. Firestore and incorporates multiple services like databases, notifications, analytics, ML, etc. Firestore is a NoSQL database that is part of the Firebase app development platform.


2 Answers

Firestore now supports "IN" queries for this purpose.

The query would look like this:

database.collection("collectionName").where("fieldName", "in", ["fieldValue1", "fieldValue2"]); 

You can have up to 10 values (fieldValueX) to check "IN" of.


The code OP desired would be as follows:

database.collection("posts").where("blogId", "in", ["1", "2"]);  
like image 76
Albert Renshaw Avatar answered Sep 22 '22 21:09

Albert Renshaw


You could combine the Observables and return as one

orQuery(){      const $one = this.afs.collection("posts", ref => ref.where("blogId","==","1")).valueChanges();     const $two = this.afs.collection("posts", ref => ref.where("blogId","==","2")).valueChanges();      return combineLatest($one,$two).pipe(         map(([one, two]) => [...one, ...two])     ) }  getOr(){     this.orQuery().subscribe(data => console.log(data)) } 
like image 22
phicon Avatar answered Sep 23 '22 21:09

phicon