I have some data on my project firestore. It seems like this:
I am trying to sort this data by SKU. As you can see it is in a list. How can I give its field path to orderBy() method?
There is no way to sort the document you've shown on a SKU value, since that value exists in an array field and there could be multiple values.
If you want to sort on a specific SKU value, add a top-level field (e.g. skuForOrdering) to the document and sort on that with orderBy('skuForOrdering').
Related
.collection('chats')
.where('members',
arrayContains: List.of([user1,user2]))
.snapshots();
I need to get a specific document, where 'members' contains exactly 'user1' and 'user2' only, and not just one of the values.
Also very important that the order of the values inside the 'members' array/list should be irrelevant..
Because this method using arrayContains is giving me any document that contains either one of these users.
What you're trying to do isn't possible exactly as you've specified. But you can do this if you assume the order of the elements in the array. For that, use a normal isEqualTo filter, providing the exact contents of the array to match. I don't write dart, but it should go something like this:
.where('members', isEqualTo: List.of([user1,user2]))
You can make sure the array always has a consistent order by sorting the items in the array before writing the field, and sorting the array you use in the query.
If the original order of the array is important, then maintain a duplicate field with the same array elements that are always sorted, and query that field instead of the original field.
See also:
FireStore query how to query arrays from the database
I've a problem with a query condition. I would like to query a string inside an array that's also inside a map. I manage to receive the information complet without the where condition but when I do this condition I've an error.
My function to retrieve all information
My array with a map inside
If category is the only value inside the produits array items, you can check for its existence with array-contains:
collectionRef.where('produits', arrayContains({ 'categorie': 'Alimentation' }))
If there are more properties for the array item, you will need to specify all of them in the call to arrayContains. There is no way to query for a subset of the array item.
In such cases, a common workaround is to add a extra array field to the document with just the property values you want to query for. For example:
produitsCategories: ['Alimentation', '...']
With that field in place you can then query it with:
collectionRef.where('produitsCategories', arrayContains('Alimentation'))
This is how my data look like.
I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!
I tried using the following code:
firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
.update({
'rating.0': FieldValue.increment(1),
});
But the whole rating became empty as seen
You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.
Is there a way to sort documents in collection chronologically when they are created? Currently, they are all over the place. For example, in To-Do app, when you add new item to collection, it should display at the bottom, last, not somewhere in the middle.
You will need to define an order based on some data in the document, and order your queries based on that field.
The typical solution for time-base order to make sure your documents all contain a timestamp field that you can use to sort them. When you call add() (or other methods to update data), you can tell Firestore to use the current time using FieldValue.serverTimestamp():
collection(...).add({
..., // your other fields
createdOn: FieldValue.serverTimestamp()
})
Then you can use that field to sort when querying with orderBy():
collection(...).orderBy('createdOn')
Try using DateTime.now() for the document ID. This should put the collection in chronological order.
For example:
Firestore.instance.collection('Posts').document(DateTime.now().toString()).setData({});
I would like to check if a certain value is present in my Cloud Firestore collection through all the present fields and have back the document ID that has at least one field whose value is the one searched.
In this example, the code should give back only 2 records when I look for "Peter": 8cyMJG7uNgVoenA63brG and fnk0kgW7gSBc3EdOYWxD.
I know how to do a search when the field name is known. But in this case, I cannot know the field name at prior.
If you don't know the name of a field, you can't perform any queries against its value. Firestore requires queries to use some index, and indexes always work with the names of fields in your documents.