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 :)
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.
Firebase is a NoSQL database; MySQL, as the name suggests, is a SQL database.
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.
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.
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"]);
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)) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With