I have an App where there are Documents for each car. I somehow need to query the Documents in a Collection to give me the ones where a User has worked on some Tasks.
The Problem is however, the Fieldpath in each Document is different so I cant query with a static Fieldpath.
Here is the Example Structure of an Document, and yes I know it is one of the worst ways to do it but I already made it a long time ago.
So what would be the best way to get the worker Array Queried if the FieldPath is not static?
The Problem is however, the Fieldpath in each Document is different so I cant query with a static Fieldpath.
If you don't know the full path of a field value to query, you simply can't query it. There are no wildcards for document fields in a query. It sounds like you will have to change your document structure if you want to perform the query, or at least just duplicate some data into another field that lets you make the query you want. Duplicating data to satisfy queries is common in NoSQL type databases.
Related
is there any way to get documents from firebase by list of IDs?
I have this list:
val IDs= listOf("id1","id2","id3","id4")
and I want to get all those documents without looping through them.
something like this, if possible:
Firebase.firestore.collection("users").documents(IDs).get()
The Firestore client libraries don't have the notion of batch reads which would allow to retrieve a set of documents given it's ids. Nonetheless, you can still make queries that filter on the document id by using the FieldPath.documentId() object. This value translates to a special sentinel that allows queries on the document id so you can write a query like below:
Firebase.firestore.collection("users")
.whereIn(FieldPath.documentId(), listOf("id1", "id2", [...], "id10"))
There's one restriction to note with this approach. The whereIn filter accepts at most 10 values. If you need to retrieve more it would be necessary to make several queries.
Alternatively, the approach suggested by Brettski of making a bunch of single document reads would work too.
That isn't possible in Firestore. Id's can only be accessed directly through a collection/document request.
I don't know Kotlin, though in JavaScript I would perhaps approach it by creating an array of functions for each of the documents, then using a Promise.all() to retrieve them at once. It is, imo a downside of Firestore where you can't query for multiple Id's.
My Android app has documents saved in Firestore following the path val documentName = "pets/$userid/$animal/$animalId". Animal can be cat, dog, bird, etc. I want to get all the pets owned by a specific user and then I want to iterate through the list of pets. How do I do that? I haven't been able to find an example. So basically I have
firestore.document("pets/$userId").get().addOnSuccessListener {
// now what?
}
There is no way to loop over all collections in the client-side SDKs, and also no way to query across collections with different names.
So the only real option is to perform a separate query for each subcollection:
firestore.document("pets/$userId/cat").get()...
firestore.document("pets/$userId/dog").get()...
firestore.document("pets/$userId/bird").get()...
And then you'd merge them client-side.
I'd also consider whether you really need these subcollections though. You can also store all of them in a single subcollection, and use a field to differentiate between animal types. That is a simpler model, which probably will work fine for this use-case.
You can't query across multiple subcollections in a single query. A query has to target a single collection or subcollection (or a collection group query that targets all collections with the exact same name). Also Firestore client SDKs can't list subcollections under a document.
You should probably restructure your data to support the queries you intend to make, which means putting the entire list of things in a single subcollection with a predictable name.
I have a simple Database structure, I want to do a query on it :
get the article wherein motcle contains.(key), and from the articles found we order them by the highest note.
I know that to do the "AND" I have to make a nested query. but how can I get the first node the article when I am in the second node motcle.
the Database
Did you consider changing the way data is structured, e.g. storing "html" is your case as value istead of key? (or migrate to Firestore, which provides better querying capabilities)
Then you could do query similar to
ref("article").child(id)
.orderByChild("motocle/someKey")
.equalTo("Html")
This is typical approach with Firebase, when data is structured based on query you need.
I am making a list activity that will contain 'achievements'. Each achievement is a record in a sqlite db in the app. In each record, I have a column with a query string stored. For each record in the db, I am using the query string against another user generated db to determine which achievements have been accomplished...
The query strings I have been using are working correctly in my sqlite manager program... however in my app, it appears that the query is being ignored and returns the entire user generated db. I'm sure there is the potential for other general errors (like null query string returned etc) but I couldn't find any, and right now I don't have my code here to post.
Are there any pitfalls I am falling into by executing a query from a string extracted from the achievement db? This was the most straightforward way I could envision doing the achievements without a whole lot of if-then clauses.
EDIT: In the end I found an error in the call, passing the wrong argument. Pitfall in the end was working too bleary-eyed.
So basically your data is denormalised. This makes it harder to change, if you ever need to change the format for example. It will also be harder to do a variety of things with your data, e.g. query the number of people with a given achievement.
I need to store an retrieve a vector of an unknown number of objects in an android sqlite database.
Essentially, the setup is this: I am developing a task management app, where the user can add as many notes as they like to their tasks. My current setup uses one database, with one row per task. This presents a problem when I need to associate multiple notes and their associated information with one task. I can see two approaches: try to store an array of notes or a vector or something as a BLOB in the task's row, or have another notes database in which each row contains a note and it's info, as well the id of the task which the note belongs to. This seems a little easier to implement, as all I would have to do to retrieve the data would be to get a cursor of all notes matching a particular id and then iterate through that to display them to the user. However, it seems a little inefficient to have a whole new database just for notes, and it makes syncing and deleting notes a little more difficult as well.
What do you think? Is it worth it to have a separate notes database? Should I use a BLOB or go for the separate database? If a BLOB, are there any good tutorials out there for storing and retrieving objects as BLOBs?
It sounds like you need another table in your database (not another database). You already have a table for Tasks. Now make one for Notes. Make a column be a foreign key into the Tasks table. That is, Notes.Task_ID would hold the ID of the Task that the Note is for. Then when you want to get all of the notes for a task, query the Notes table.
I think the answer to this question really lies in how you're going to go about updating things should they change. For now, the BLOB route probably seems like a really good idea, but what happens if you want to add some new functionality and you want to store some new property of notes (think of things like starred or importance). What would you need to do in order to update the notes object to add this new field? If it's just a database table, it's quite easy to change the layout of the table and even add a default value. If it's a BLOB, you're going to need to go through each entry, de-serialize the BLOB object, fix it, and re-serialize. That could get tricky.
Also, and this probably isn't as important to a small application using an embedded database, but it's easier to modify the database outside of the application if the object isn't a BLOB. Not to mention the queries you'll be able to write with the separate table. For example, how might someone calculate the number of notes that are attached to a task? If it's separated out in the database, it's a simple query.
Just my two cents.