where _parentId is the partition value I use to access some objects of the collection, like this:
self.realm = try! Realm(configuration: realmApp.currentUser!.configuration(partitionValue: "myPartitionValue"))
// Access all students in the collection matching the partition value
self.students = self.realm?.objects(Students.self).sorted(byKeyPath: "lastName")
I would like to access all the students in the collection, regardless of their _parentId (partition value). I tried to do the followings but I got nothing:
First try:
Realm.asyncOpen() { (response) in
let students = try! response.get().objects(Students. self )
}
Second try:
let realm = try! Realm()
let students = realm.objects(Students.self)
Do you know how can I get all the Students without iterating on all the different partition value (i.e _parentId)?
You can’t really get there from here. The partition key is what encapsulates data into Realms - e.g. you can think of a partition as a Realm. Realms are separate, discreet sets of data and you can’t access data across Realms at the same time. The actual file structure on your device has the different partitions (.realm files) stored in different files as well.
You can however (as you mentioned), access data in Realm A, then access data in Realm B etc.
Also, there are some options with server side code but there may be other options with some additional clarity in the use case.
Why would some students have one partitionKey and other students have a different partition key. Why not use the same partition key for all students ‘students_partition’?
My students have different partition key because they belong to different groups (one for each partition key). So, usually I need to get only the students of one group but I wanted to implement the possibility to search among all the students (regardless on their groups), but to do that I need to get all of them.
Part of working with NoSQL databases (which Mongo is under the hood) is organizing data based on what you want to get out of it - your queries. In this case, one parameter is being able to search through all students. That would require a different setup
I would suggest keeping all of the students in the ‘students_partition’ and then keep references to the students within they group the belong to. Conceptually it would be:
students_partition
student_0
student_1
student_2
groups_partition
group_0
students
ref_to_student_0
ref_to_student_1
or you could reverse that or even augment it by keeping a reference to the group with each student - it really depends on what additional queries you may want to run.