I’m using MongoDB Realm and the Atlas UI for a Swift app mapping US States with congressional districts (I’ve used Region to refer to states to avoid confusion with the swift keyword @State), so each region should have a list of districts. I can insert starting data on regions and districts into the Atlas UI using PyMongo and display those states and districts individually when pulling realm results in my swift app, but when I pull the region it does not show any associated districts in its list of districts.
For reference, here is how I defined my region and district model
class Region_Preset: Object {
@objc dynamic var id_number = 0
@objc dynamic var _id = ObjectId.generate()
@objc dynamic var _partition: String = ""
@objc dynamic var Abbreviation: String = ""
@objc dynamic var Name: String = ""
let Population: Int? = 0
let Districts = List<District_Preset>()
let Senators = List<PoliticianUnverified_Preset>()
let Representatives = List<PoliticianUnverified_Preset>()
let Voters = List<Voter_Preset>()
override class func primaryKey() -> String? {
return "_id"
}
convenience init(id_number: Int, partition: String, Abbreviation: String, Name: String) {
self.init()
self.id_number = id_number
self._partition = partition
self.Abbreviation = Abbreviation
self.Name = Name
}
}
class District_Preset: Object {
@objc dynamic var id_number = 0
@objc dynamic var district_id: String = ""
@objc dynamic var _id = ObjectId.generate()
@objc dynamic var _partition: String = ""
@objc dynamic var Number: String = ""
let Population: Int? = 0
let Zipcodes = List<Zipcode_Preset>()
let Voters = List<Voter_Preset>()
@objc dynamic var Representative: PoliticianUnverified_Preset?
override class func primaryKey() -> String? {
return "_id"
}
convenience init(id_number: Int, district_id: String, partition: String, Number: String, Representative: PoliticianUnverified_Preset) {
self.init()
self.id_number = id_number
self.district_id = district_id
self._partition = partition
self.Number = Number
self.Representative = Representative
}
}
And here are the Pymongo functions
def upload_regions():
for region in region_file:
new_region = { "id_number": region[0], "_partition": PARTITION_VALUE, "Abbreviation": region[1], "Name": region[2]}
db.Region_Preset.insert_one(new_region)
def upload_districts():
for district in district_file:
new_district = { "id_number": district[0], "district_id": district[1], "_partition": PARTITION_VALUE, "Number": str(district[2]) }
db.District_Preset.insert_one(new_district)
returned_district = db.District_Preset.find_one({"id_number": district[0]})
db.Region_Preset.update_one({"id_number": district[4]}, {"$push": {"Districts": returned_district}})
And like I said the results of the Pymongo insertion appear in the Atlas UI
Sorry I’m unable to post inline pictures because I just started my Stack Overflow account, but you can see both that regions and districts appear inserted in Atlas UI and even (from the 2nd picture) that the districts appear to be adequately inserted into the region’s array of districts.
When I pull this data and print it in my Swift code, however, as shown in this code
Realm.asyncOpen(configuration: userconfiguration) { result in
switch result {
case .failure(let error):
print("Failed to open realm: \(error.localizedDescription)")
// handle error
case .success(let newrealm):
print("Successfully opened realm: \(newrealm)")
// Use realm
if let user_id = app.currentUser?.id {
let regions = newrealm.objects(Region_Preset.self)
print(regions)
I get the following output from the print statement
Results<Region_Preset> <0x7f931b325330> (
[0] Region_Preset {
id_number = 1;
_id = 5fe0f0c6474d38a0a1c11166;
_partition = vote;
Abbreviation = NY;
Name = New York;
Districts = List<District_Preset> <0x60000095b520> (
);
Senators = List<PoliticianUnverified_Preset> <0x6000009440a0> (
);
Representatives = List<PoliticianUnverified_Preset> <0x6000009441e0> (
);
Voters = List<Voter_Preset> <0x600000944280> (
);
}
)
Which shows the existence of the region in Atlas UI (and the districts when I print that instead individually), but does not show any districts associated with the region.
Am I doing something wrong here in the python code, the Atlas UI, the swift code or somewhere else? I assume this functionality should definitely be supported by Pymongo/MongoDB Realm but I can just not get it to show - any help is greatly appreciated!