This is a repost of a question I asked in the wrong forum. I’m reposting here since it has much more to do with the Swift SDK than MongoDB Realm in general.
I’m working on a Swift iOS app using Realm Sync and MongoDB Atlas. It’s a photo editing app, and I want people to be able to create filters that they have write access to, and be able to share them, so that other users can have read-only access to them to download them on their phone.
I’m able to sign in, open a realm, create filters, store them, and access them.
However, I’d like to run a query for all filters available to download (i.e. those which aren’t owned by me). My data is partitioned by the user_id
property.
Here is the schema for my filters:
{
"title": "DFFilter",
"bsonType": "object",
"required": [
"_id",
"user_id",
"name"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"user_id": {
"bsonType": "string"
},
"name": {
"bsonType": "string"
},
"edits": {
"bsonType": "objectId"
}
}
}
And here is my equivalent swift Object:
@objc public class DFFilter : Object {
@objc dynamic public var _id: ObjectId = ObjectId.generate()
// Partition Key
@objc dynamic var user_id: String = ""
@objc dynamic public var name: String = ""
@objc dynamic public var edits: DFEdits? = nil
override public static func primaryKey() -> String? {
return "_id"
}
// ...
And here is how I’m performing the query:
let collection = database.collection(withName: "DFFilter")
let filterDocument : Document = [
"user_id": [
"$ne": AnyBSON(forUser.id)
]
]
collection.find(filter: filterDocument) { result in
switch result {
case .failure(let error):
print(error)
case .success(let documents):
print(documents)
completion(documents.map({ document in
let dfFilter = DFFilter(value: document)
print (dfFilter)
return "foo" // just a placeholder for now
}))
}
}
However, the initializer is failing to create a local unmanaged DFFilter object from the BSON Document I’m getting from Realm:
Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value 'RealmSwift.AnyBSON.objectId(6089b6e38c3fafc3e01654b1)' of type '__SwiftValue' for 'object id' property 'DFFilter._id'.'
Here’s what the BSON document looks like when I print it in the console:
(lldb) po document
▿ 4 elements
▿ 0 : 2 elements
- key : "_id"
▿ value : Optional<AnyBSON>
▿ some : AnyBSON
- objectId : 6089b6e38c3fafc3e01654b1
▿ 1 : 2 elements
- key : "name"
▿ value : Optional<AnyBSON>
▿ some : AnyBSON
- string : "Hey Hey"
▿ 2 : 2 elements
- key : "user_id"
▿ value : Optional<AnyBSON>
▿ some : AnyBSON
- string : "6089b62f9c0f6a24a1a5794b"
▿ 3 : 2 elements
- key : "edits"
▿ value : Optional<AnyBSON>
▿ some : AnyBSON
- objectId : 6089b6e38c3fafc3e01654b2
I’ve tried search around for answers but I’m coming up blank. This indicates to me that potentially my whole approach to this problem might be mistaken?
It is worth pointing out that the edits
property of DFFilter
which you see in the schema referenced by an object_id is a different object of type DFEdits
. I’m also not sure how I can query an object and its dependencies at once? Does MongoDB resolve these automatically?
It seems like it might even be easier to just write a GraphQL query directly and decode the response into my local object types?
Just not really sure which direction to head.