Collection.upsert() does not insert a document if it is not found.
tomlandrum opened this issue · comments
Describe the bug
Performing an upsert operation does not insert the document if it is not found.
To Reproduce
Attempt to perform an upsert operation with a document that does not exist in the collection.
Expected behavior
If the document does not exist in the collection, it should be inserted.
Server-side Swift:
- OS: macOS
- Swift Version: 5.1
- MongoKitten Version: 6.0.0
This is the code I'm executing:
let encoder = BSONEncoder()
let document = try encoder.encode(item)
let future = db?[collectionName].upsert(document, where: "_id" == document["_id"])
The operation returns ok, but the document is not inserted.
I modified UpdateCommand.UpdateRequest (MongoKittenCore/Commands/Update.swift) to include the upsert variable in the CodingKeys enum, and that fixes the problem for me. I have a PR ready to submit.
Whups, that makes sense. Let me check if I already have a patch for that. I'm running on a beta build myself, maybe that's why
Your PR is very welcome btw, and thank you for finding it :)