const data: any = await db.listCollections();
I need to get only limited number of collections, you can say pagination. But I didn't find any firebase query with limit on getting collections . Because the db is design like this way that you have to get all the collections, and there is too many collections.
When using Firestore you should typically give your collections known names, so that you can repeat them in the client-side code, and generate the names for the documents.
The Firestore API isn't well suited for listing or querying collections.
I recommend pivoting, and storing the information that requires listing into documents, so that you can use the query and pagination APIs on documents.
Related
I could not find anything in the docs, but say I perform a query such as
const snapshot = await jobsRef.where('status', '==', STATUS_ACTIVE).get();
Could exclude certain fields from being returned for this "job"?
There are certain fields in the "job" document that should remain private.
For web and mobile clients, it's not possible to exclude certain fields from a query. When you query a document, it will always deliver all of the fields for all of the matching documents. Security rules will not help you with this.
For cases where there are public and private fields to separate, you should make two different collections and protect them with different security rules. You can choose either two top-level collections:
/jobs-public/{id}
/jobs-private/{id}
Or you can use subcollections:
/jobs/{id}/public/{id}
/jobs/{id}/private/{id}
In either case, you will have to make sure that the user can only read the documents that they are allowed to by your requirements.
It's not possible to retrieve certain fields and as Doug mentioned above, you can separate the fields in different collection or sub-collection.
If you do not wish to redesign your database structure, you can alternatively use Firebase Cloud Functions in which you can fetch the required document, filter the data received and return the required fields to user. Hence user won't be able to see complete Document either.
Though this will obviously increase your costs as you will be using Cloud Functions.
Though it's really effective to way filter and Verify who is retrieving the data.
I am pretty new to NoSQL and would like to fully understand the concept of namespace and how it compares to SQL schema.
I have seen plenty of useful analogies between tables, row, ... and their NoSQL counterparts.
Could you please help me understand the namespaces ?
In particular, I would like to know how I could leverage them to segregate the data of my dozen of customers ? I want to prevent accidental information leak between two of then, while still using a single database.
It really depends of the database engine you are using, it is hard to give a generic answer.
Ideally, if you really want to segregate the data, you can use multiple databases (in Redis, Redis Enterprise, MongoDB). In this case you are sure that data are separated. But you say you want to use a single DB. (why?)
If you want to stick with a single database you have various options, once again depending of the database engine you are using.
If you are using Redis:
you can use specific namespace based on key pattern, for example app:cust-001:orders, and you control the access to the data based on the key name/pattern. In Redis 6.0, the notion of ACL (Access Control List) has been added allowing you to limit the operations/access to the data based on a key pattern, for the connected user. This will allow you to have a good control of the data and who can see/manipulate them
If you are using MongoDB:
you can use multiple collections (tables), for example, prefixing the collection name with a context.
or you can use a composite key, where one of the fields will be your context
In both cases, for Redis and MongoDB, you are kind of creating using business logic the concept of "database".
If you provide more details/examples, the community can probably give you a more detailed answer.
I have a dilemma on how to solve possible redundant data querying.
I am using MongoDB with Apollo server and client. My MongoDB has several collections of data. The main collection consists of IDs pointing to supporting collections.
I am not sure about how to solve the mapping of IDs of my main collection to supporting collections IDs to retrieve the actual values. The thing is that mostly I already have data of supporting collections cached in Apollo client cache.
Do you think I should only query the IDs in my main collection and map IDs to values on the frontend using cached data? Or should I have a resolver that takes IDs in main collection, makes database queries to supporting collections to get value for each ID and then sends prepared data to frontend?
I appreciate any insight! Thank you.
As always, it depends. I assume that this is your setup, with a main collection.
type OtherDoc {
id: String
field: String
}
type MainDoc {
id: String
otherDocs(param: String): [OtherDoc]
}
type Query {
mainDocs: [MainDoc]
}
In such case, querying for mainDocs { id otherDocs("...") { id field } } is definitely a natural way to get this data. It might be redundant, in terms of getting OtherDoc when different param result in the same docs. If so, you may think about querying only their IDs and then querying for separate docs, if the client doesn't have them.
I'd say it's a valid solution, but definitely not something you should consider from the beginning. This optimization will definitely limit the bandwidth, but increase the number of requests. What is more, you don't know when to actually refetch OtherDoc. Well, maybe you do, but you have to think about and build it, where without you have it out-of-the-box.
A different approach, a more cache-friendly one, may change the schema to limit such situations, where your data overlap. This is not always possible due to the business logic, but worth considering if it is.
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.
Data structure example
I have a simple data structure, collection of wallets and collection of transactions in each wallet, what a simple way to get all transactions from each wallet using angularfire2?
There is currently no way to query across multiple collections with the Cloud Firestore SDK. If you want to access the data from multiple collections, you will have to load the data from each collection and then handle the rest in your application code.
Alternatively you could keep all transactions in a single top-level collection and associate them with their wallet through a field in the document.
A final option is to keep the transactions in both places: both in a global list and for each user. While this doubles the amount of data you store, it'll likely make lookups easier, and faster.