I'm using for first time collection groups querys on firestore in my web application but I can't retrive any data and I don't know why
I'm using Firebase SDK 7.6.1 and I have created the indexes correctly i think.
This is my Collection data structure:
And this are my Indexes:
And finally the code with I'm trying to retrieving data is:
doc = this.database.collectionGroup('Authors')
.where('Name','==',this.TextParams.trim().toLowerCase());
I expected to retrieve the documents where the author name is equal to my TextParams variable
In your screenshot, Authors is not a subcollection, it's an array type field. Since it's not a subcollection, a collection group query won't work at all. Also, it's currently not possible to search among array field items.
What you will have to do is actually put your Authors into a subcollection.
Related
I try to retrieve all the grand parent objects when a grandchild key is matched. In the Firebase realtime database below, I have for example the sensor key "-LNVBiM1SGoAFvlMfi1V", and I need to retrieve with it all the orders objects where this key is found (04TBR7 in this example).
I know the database structure might not be perfect but there is no possibility to modify it for now.
I tried the following code but it is not right, I guess the equalTo is not working since it is an Object:
db.ref('/orders').orderByChild('sensors').equalTo(sensor_id).once('value', snapshot => {
// my snapshot is always empty ...
});
There is no way to use orderByChild("sensors") in the way you're trying, as it checks the value of the sensors property (not the key).
It's important to keep in mind that Firebase database queries work on a flat list of nodes. So you can search for a specific order property across all orders, and you can search sensors under a specific order. But you can not search across all sensors for all orders.
Since you say you can't modify the data structure, that unfortunately means you'll have to load the entire JSON and find the matching nodes client-side.
Also see:
Firebase query if child of child contains a value
Firebase Query Double Nested
I want to store the comma separated ids on a child node & how can I filter data as in sql we can use IN clause to fetch data any possibility in firebase to perform this kind of operation in firebase database.
Please suggest any possible solution for this.
Firebase Realtime Database doesn't have the equivalent of SQLs IN clause. It also doesn't have a way to find a substring in a value. So the data model you are looking to use, doesn't allow the use-case you want. As usual with NoSQL databases, the solution is to pick a data model that does allow your use-case..
The most likely cause I know for the structure you describe is to associate the child node with a bunch of categories. If that is your case, read my answer here for a proper data structure: Firebase query if child of child contains a value
This is one of the cases where the new Cloud Firestore database offers better querying support, since it recently added a feature to efficiently test if an array contains a certain value (video). If you're only just getting started with your project, you might want to check if Firestore is a better fit for your use-cases.
How can I order the documents in Firestore? It looks like Firestore lists documents in alphabetic order by the ID I let Firestore automatically create. But I don't want that. I just want to see my newly added document added as the last document in the collection. How do I do that? Should I create my own alphabetic ID's?
Example of my collection in Firestore:
Firestore is meant to scale massively. At that scale, it doesn't matter what order the documents appear in the console, because the console is useless for viewing massive numbers of documents within a collection.
If you still need an ordering in the console, you will have to come up with document IDs of your own.
If you have a timestamp field, you can add a filter on timestamp and sort results by desc. Do not add a filter condition.
How does one select the last n items in a collection in firestore? I'm considering migrating a new project from the real time database to firestore. In the realtime database, I used the below code:
ref.orderByKey().limitToLast(n);
I've reviewed the docs at https://cloud.google.com/firestore/docs/query-data/order-limit-data, and they don't appear to mention how to achieve this in firestore. I suppose I could add a timestamp field to every element of the collection and then order by descending timestamp, but I think there should be an easier way to accomplish this.
In the Firebase Realtime Database there was the concept of a push ID, which was a key that was automatically chronologically ordered. This made ordering nodes chronologically in a query as easy as you've shown.
In Cloud Firestore there is no built-in metadata for when a document was added, or when it was last updated. If you want to build queried based on such metadata, you will have to set the corresponding properties on the documents yourself.
plnkr
I am trying to traverse through a collection, and update each document respectively.
My UserProfile collection consists of multiple JSON objects of userProfiles. As you can see, each profile has a lot of the same information. The only difference is the personal information. (This is just a test case of hard coded objects. The real data will be in an SQL DB managed by a sysadmin).
What I am trying to do is write a function (replaceTopics) that will take in an array of topics and replace each topic that matches in the collection. So if the system admin makes a change to a topic/s, he will send me the topic/s and I will be checking each document in my userProfile collection to see if that document has the matching topic (by matching topicIDs), if so, I need to replace that entire topic with the editedTopic.
I have tried this but with no luck. You can take a look at my function.