Firestore delete an index inside an array - flutter

i am trying to delete an index inside array
this is the data structure
I want the user to delete the selected car so it could be index 0 or whatever

My opinion is fetch the document and delete the item at which index you want from the local array and update the remote document. Or you can use .document('docID').updateData('cars': FieldValue.arrayRemove([{the-item-map}]));. Maybe there are other options but these two options are acceptable, I prefer to use first one.

Related

add string to an array in Firestore - flutter

i have created an array of strings "challengeMember" in Firestore database and i want to add data to it when user clicks a button, my database:
in my code am trying to update the array, every time user join a challenge, the challenge name will be added to this array i know how to access the array using:
FirebaseFirestore.instance.collection("Users").doc(user!.uid).update({'challengeMember': widget._challengeName});
but the problem with this it is not specifying the array index and it is not checking whether the index is empty or not, so how can i dynamically access this array and keep adding values.
Firestore has a special trick to add an array, and it goes like this:
await FirebaseFirestore.instance.collection('users').doc(uid).update({
'tokens': FieldValue.arrayUnion([token]),
});
See the FieldValue documentation on how to manipulate arrays.

How to increment individual element in an array in firestore for flutter

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.

How Can I Create a rule in firestore security rules for each item in my array

I can access a single item with its index like this
In this example i can access a single item in that array but i want the rule to be not for that single item but for each item in that array.
After looking for a solution for some time couldn't find anything and instead of storing the Posts in an array i just created a collection inside the Person document and stored them in there.

mongodb multiple documents insert or update by unique key

I would like to get a list of items from an external resource periodically and save them into a collection.
There are several possible solutions but they are not optimal, for example:
Delete the entire collection and save the new list of items
Get all items from the collection using "find({})" and use it to filter out existing items and save those that do not exist.
But a better solution will be to set a unique key and just do kind of "update or insert".
Right now on saving items the unique key already exists I will get an error
is there a way to do it at all?
**upsert won't do the work since it's updating all items with the same value so it's actually good for a single document only
I have a feeling you can achieve what you want simply by using the "normal" insertMany with the ordered option set to false. The documentation states that
Note that one document was inserted: The first document of _id: 13
will insert successfully, but the second insert will fail. This will
also stop additional documents left in the queue from being inserted.
With ordered to false, the insert operation would continue with any
remaining documents.
So you will get "duplicate key" exceptions which, however, you can simply ignore in your case.

MongoDB: upsert with different values for update and insert

A little context: I have a document for each user that contains an array with latest 20 events related to a user. As MongoDB does not have this feature(to cap arrays inside a document), I will push my event and pop the latest one.
My problem: initializing the document(aka filling array with nulls). I want to atomically:
create document containing an array with 20 null values and push one value, if document does not exist
or
update document (push one value in array), if document exists
Do you have any other suggestions? A hack I thought about would be to declare a index with :unique and :dropDups, and to always make an initialization insert.
Related to: MongoDB fixed size array implementation
Not possible in a single operation, yet. You want http://jira.mongodb.org/browse/SERVER-991 or http://jira.mongodb.org/browse/SERVER-453.