Counting fields (specifically arrays) in a document - swift

I have my document setup as:
Collection
------> Document
------> Array 1
Array 2
Array 3
I need to be able to count the number (as an Int) of arrays (or fields) in this document and be able to use it in a SwiftUI ForEach statement.
The problem is, I can't just use a specific number, because arrays are constantly being added and removed.

Since the arrays are constantly being added and removed, then you have two ways in which you can solve this. The first one would be to loop over the fields inside the document and check if the field is an array or not. Or you can simply increment/decrement a counter each time a new array is added or removed.

Related

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.

How does addToSet determine a duplicate?

Say I have a document that contains an array named Months. Each element in the array has a int field indicating which month & year it refers to (yyyyMM), e.g. 201612, 201701, etc.
Each such element also contains additional fields, pertaining to that particular month. e.g. int numOfItems, string Country, etc.
Now, assuming I have such document with only 2 elements in the array:
Months[0].month == 201612
Months[1].month == 201701
Then, I call addToSet with Months[1].month == 201701.
How does addToSet determine whether this is a duplicate or not?
Does it inspect and compare all sub-elements of Months[1] to do that? Does it only inspect the index of the element in the Months array?
This is directly from MongoDB.
MongoDB - AddToSet
If the value is a document, MongoDB determines that the document is a duplicate if an existing document in the array matches the to-be-added document exactly; i.e. the existing document has the exact same fields and values and the fields are in the same order. As such, field order matters and you cannot specify that MongoDB compare only a subset of the fields in the document to determine whether the document is a duplicate of an existing array element.

Searching for subrow in an array

I have a problem with swift. I want to search an array of strings consists of 3 different elements filling this array in certain manner. Next i want to search the array for a subset of 3 particular strings next to each other and return their indexes. Is that a special array method for this?
Copied from comment:
Assumed i have an Int array like [1,2,1,3,2]. I want to search it for a subarray [1,2,1] and return indexes of those elements. Should i use findwithPredicate method?
I would just iterate the Array checking for matches.
Check the items in the array in sequence comparing the current item with the first item of the subarray. If it does not match move on. If it does start comparing the rest of the subarray, if it fails go bad to scanning the array.

Mongo query for number of items in a sub collection

This seems like it should be very simple but I can't get it to work. I want to select all documents A where there are one or more B elements in a sub collection.
Like if a Store document had a collection of Employees. I just want to find Stores with 1 or more Employees in it.
I tried something like:
{Store.Employees:{$size:{$ne:0}}}
or
{Store.Employees:{$size:{$gt:0}}}
Just can't get it to work.
This isn't supported. You basically only can get documents in which array size is equal to the value. Range searches you can't do.
What people normally do is that they cache array length in a separate field in the same document. Then they index that field and make very efficient queries.
Of course, this requires a little bit more work from you (not forgetting to keep that length field current).

How to exclusion join 2 NSArrays?

I've got 2 arrays of objects (as NSArray's) and I want to get an array of the objects in the first that aren't also in the 2nd.
See NSMutableArray's -removeObjectsInArray: method.
1) Inefficient solution. run over one array and call on each object containsObject if no do add the element otherwise take the next element
2) More efficient: sort both array and then step them side-by side. Keep a reference to the last added object and then compare the next components to this element. If none matches you can add a new "last" element.
3) Drop all the Elements in a HashTable then take the next elements of the next array and check whether you can find an "equal" object. At the end rebuild an Array from the HashTable
4) drop all the elements into an NSSet and after that rebuild an Array from this NSSet