Reference nested documents in swift SDK collection.findOneDocument()

In Swift, I’m querying for a document like this:

collection.findOneDocument(filter: queryFilter) { result in
                switch result {
                case .failure(let error):
                    print("Did not find a document")
                    return
                case .success(let document):
                    print("Found a matching document: \((document?.first?.value)!)")
                }
            }

But my document has 2 arrays of nested documents that are returned as just arrays of id’s. How do I populate the nested documents in the query?

I appears you’re using the Swift SDK, not the RealmSwift SDK - you may want to take a look a the RealmSwift SDK as it adds a few more features and makes queries a bit simpler and getting back deep associated data more clean (just IMO)

We will need a bit more info though - the question states the arrays contains nested documents but then says it’s an array of id’s, and it can’t be both, well, it can be.

If it’s an array of ID’s (as just text for example) you may need to craft a separate query to retrieve those.

If it’s an array of documents then well, there they are!

On the other hand, you may just need to unwind the array to get the documents. See Unwind Array Values and see if that applies.

I think the question needs more data - can you edit it to include the structure of your document. Including your Realm Models would help as well.

Thanks so much for the reply, @Jay . Here’s some more info.

The models.

Recipe:

_id: String? = ObjectId.generate().stringValue
author: String = ""
title: String? = nil
directions = RealmSwift.List<Direction>()
ingredients = RealmSwift.List<Ingredient>()

Ingredient:

_id: String? = ObjectId.generate().stringValue
author: String = ""
name: String? = nil

Direction:

_id: String? = ObjectId.generate().stringValue
author: String = ""
text: String? = nil

For most purposes, I synch with author as the partition. But in this edge case, I want to access a Recipe document that is outside of the current user’s partition. I’ve tried a few different angles. But this has been the most promising. Here, I’m just logging the nested ingredients documents as I try to figure out how to unpack them.


let client = app.currentUser!.mongoClient("mongodb-atlas")
let database = client.database(named: "funky_radish_db")
let collection = database.collection(withName: "Recipe")
            
let queryFilter: Document = ["_id": "617bf8c26bd5c64cafe9233e"]
            
collection.findOneDocument(filter: queryFilter) { result in
     switch result {
          case .failure(let error):
               print("Did not find matching document")
               return
          case .success(let document):
               print("Found a document: \((document?["ingredients"]))")
           }
     }
}

But this is what I’m getting back:

**Optional(Optional(RealmSwift.AnyBSON.array([Optional(RealmSwift.AnyBSON.string("617d78976200a9a598c9ca44")), Optional(RealmSwift.AnyBSON.string("617d78976200a9a598c9ca45")), Optional(RealmSwift.AnyBSON.string("617d78976200a9a598c9ca46")), Optional(RealmSwift.AnyBSON.string("617d78976200a9a598c9ca47")), Optional(RealmSwift.AnyBSON.string("617d78976200a9a598c9ca48"))])))**

So I’m trying to make that next jump from this optional array of optional RealmSwift.AnyBSON.string, to the documents with the included data.

I ended up solving this here