No aggregation lookup at v 6.0.1
JoachimM7 opened this issue · comments
There seems to be a change in the API in v. 6.0.0 (I have to use the beta, because in my project I use SwiftNIO 2.8.0). Why is there a change back to AggregateBuilderStage?
I can live with it, but now there is no lookup
in the aggregation possible anymore. Or do I overlook sth.?
Hello @Lupurus we do intend to reintroduce it, but we're very unsure what we want the API to look like.
Hey, thanks for that fast answer. Any idea how I can get it back for the moment?
I liked the API from > 5.0.0, but if I look to the MongoDB documentation, the new API with AggregateBuilderStage
seems to be more consequent!
You can make custom stages
Yes, I found it out so far. Because there is no documentation, it's not quite easy to continue ;) How do I do this?
let pipeline = self.db.aggregate([
AggregateBuilderStage(document: ["$match": "{ username: \"\(auth.username)\" }"])
])
pipeline.execute().whenComplete { result in
print(result)
}
This gives me: failure(Couldn't find type Cursor at path "["cursor"]")
Ok, I found it out. Should be:
AggregateBuilderStage(document: ["$match": ["username": auth.username]])
And by the way for everyone who needs this right now, I made an extension. @Joannis: mabye you could use this as well in the final code?
extension AggregateBuilderStage {
static func lookup(from: String, localField: String, foreignField: String, as newName: String) -> AggregateBuilderStage {
return AggregateBuilderStage(document: [
"$lookup": [
"from": from,
"localField": localField,
"foreignField": foreignField,
"as": newName
]
])
}
static func unwind(fieldPath: String, includeArrayIndex: String? = nil, preserveNullAndEmptyArrays: Bool? = nil) -> AggregateBuilderStage {
var d = Document()
d["path"] = fieldPath
if let incl = includeArrayIndex {
d["includeArrayIndex"] = incl
}
if let pres = preserveNullAndEmptyArrays {
d["preserveNullAndEmptyArrays"] = pres
}
return AggregateBuilderStage(document: ["$unwind": d])
}
}
Oh yeah, we can totally use this! That API looks nice