orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift

Home Page:https://orlandos.nl/docs/mongokitten/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Debug print of a cursor

JoachimM7 opened this issue · comments

After performing a query, the result is a MongoCursor fitting the QueryCursor-protocol. To get the data, you need to perform sth. like firstResult() to get an EventLoopFuture. With this, you can decode it to a Codable-struct. This way is perfect and works fine. But it's really horrible for debugging, if the database doesn't match the defined struct (because you don't know where is the mistake). Is there an easy way to print out the JSON-data of the result without draining the cursor? It doesn't seem so, so may I suggest to add this feature, so that you could type sth. like this?

let pipeline = collection.aggregate( ....)
print(pipeline.resultAsJSON)    // this contains a string with the JSON-result

Of course the same for the other queries like

let result = collection.findOne( ...)
print(result.resultAsJSON)

Sorry, but this is not possible. The moment the results are printed, the cursor is already drained. The only alternative could be to do the query again, but then printing the cursor's contents