I need get return data from Firebase. In Sql query it would be like this:
Select * from * Group By field1, field2 HAVING count(*)=1
How to apply above to FireBase in Xamarin Android. So I have node "chats", here I want retrieve record only not repeated on field "user_email":
chats
|-...
|---date_and_time:
|---user_email:
|-...
|---date_and_time:
|---user_email:
...
at this moment I get whole data from "chat", here code:
public List<MessageContent> listOfContacts = new List<MessageContent>();
FirebaseClient firebase;
...
firebase = new FirebaseClient(GetString(Resource.String.database_name));
...
var firebaseContacts = await firebase.Child("chats")
.OnceAsync<MessageContent>();
foreach (var item in firebaseContacts)
listOfContacts.Add(item.Object);
Firebase's realtime database is a NoSQL database. Trying to directly map a SQL query to it is going to give you a rough time. I highly recommend reading NoSQL data modeling and watching Firebase for SQL developers.
In NoSQL databases you typically get more complex write operations and in return get more performant/scalable read operations. So in your case, that could mean that you actually store a list of the records that you're trying to query and update that list with every write.
Some previous questions that deal with similar scenarios can be found in this list.
Related
I'm currently using Realtime Database, but I can't find any solution for my question.
My Inventory hierarchy in Realtime Database:
My setValue function: (realtime database)
dbRef.child("inventoryData/inventory/" + gainedItem.getDbId() + "/items/" + gainedItem.getItemId()).setValue(dbInventoryItemClass);
How can I do this in Firestore?
In Firestore, I'm storing my data in a document. But when I want to edit spesific data, I have to download all document - edit - upload again. What I want is that I just want to edit specific data like in Realtime Database.
For example, I want to remove "0" from subList, in realtime database I can remove with:
dbRef.child("inventoryData/inventory/" + gainedItem.getDbId() + "/items/" + gainedItem.getItemId()).setValue(null);
How can I do this in firestore? I can't use this code: ref.collection("users").document(*userId*+"/inventory/0/items/2....").delete();
Is there any way like I used in Firebase?
I'm not entirly familier with what you want to gain, but I'll give it a try.
with a single collection lets call it "ITEMS".
the "ITEMS" collection will hold the dbInventoryItemClass, and because we are using the firestore and not the realtime database, document read is the same regardless of the quantity of data in it (up to a certain limint)
add in dbInventoryItemClass fields like inventoryID
(that you can query with .whereEqualTo("inventoryID",0) to get all items in inventory 0.
you can of course add as may identifications and chain several whereEqualTo's, just make sure to log the exeption given in the OnFailureListiner to get the link to create the index for the chained whereEqualTo query.
as for your question:
As I undestand it, I have to download all inventoryData in Firestore. Is it true?
as far as i know, in firebase firestore when you Query document(s), you'll get ONLY the document you queried (and not any subcollection assosiated with the document)
good luck!
database structure
I'm completely new to realtime database and nosql and want some help.
How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
Firebase doesn't provide a way to filter the records by keys that start with a particular String. The most simple solution I can think of is to change the "year" property in your database to be of type number and not String, otherwise, you'll have a lexicographical order. To solve this, you need to use a query that looks like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference calendarRef = rootRef.child("calendar");
Query yearQuery = calendarRef.orderByChild("year").startAt(2000).endAt(2001);
yearQuery.addListenerForSingleValueEvent(/* ... */);
This will return all records where the "year" property holds the value of 2000.
I've been trying to find a query for almost 2 days now
I want to search id (current user id) from the document 4 fields (customer1,customer2,customer3,customer4)
Here is the firestore document picture
tried this query
final Query userQuery = collectionReference
.whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer2",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer3",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer4",firebaseAuth.getInstance().getCurrentUser().getUid());
but this only shows up if the current ID is present in all 4. Is there any easier way to do this.
You can do that by using a field that is an array containing the uids you want to test, and then applying array-contains on it. In your case:
In your case:
customer: [customer1, customer2, customer3, customer4]
collectionReference
.where("customer ", "array-contains", firebaseAuth.getInstance().getCurrentUser().getUid())
Firestore does not support logical OR queries among mulitple fields. So, what you're trying to do is not possible with a single query using the database structure you have now. You would have to perform multiple queries and merge the results in the client.
If you want to be able to use a single query, you will have to change your database. One option is to put all the customers into a single array field and use an array-contains query to find a customer in that field.
I am just getting started with Firestore. For performing Queries, Firestore comes recommended, unlike Realtime Database. Can someone clarify me on how deep I can go with using queries in Firestore?
For illustration, let us take a list of School Students. Each Document contains information about Name, Age, Gender, City, Language, Specialization. So can I perform a compound query like the following?
Query studentList = db.collection("students")
.whereEqualTo("age", 12)
.whereEqualTo("gender", "female")
.whereEqualTo("city", "paris")
.whereEqualTo("language", "french")
.whereEqualTo("specialization", "maths");
Sure, you can apply multiple conditions to a query. Each condition is logically AND'd with each other. So, the query you're showing now effectively asks for documents in the students collection where age=12 AND gender=female AND city=paris AND language=french AND specialization=maths.
Please consult the documentation to understand better how Firestore queries work.
I have Firebase data that looks like this.
I want to create a Query object in java that retrieves only Invites (those are the objects with randomly generated key's KHcYy...) which have a name = "John Smith".
Here is the code for my current attempt, which is only giving me an empty query:
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.child("brother").equalTo("John Smith");
I tried taking a look through this tutorial on queries, but that only really had info on doing sorty and ranking based queries. I couldn't find any info on this type of task, and the API Reference didn't really make it clear what I'm supposed to be inputting in any of these calls.
Thanks for the help!
Have you tried this...
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.orderByChild("brother").equalTo("John Smith");