I'm accepting Pull Request 358, which adds mongoc_collection_aggregate_with_write_concern, but I noticed a problem. The algorithm there is:
- Select a server
- If its wire version is 5+ and the user passed a write concern, append it to the command document
- Create a cursor with the command document
Then, as always, when the user runs mongoc_cursor_next, its algorithm is:
- Select a server
- Send the command document to the server
But the two selections may select different members of the replica set or different mongos servers in a sharded cluster, and these servers may have different wire versions!
Instead, let's add a mongoc_write_concern_t to mongoc_cursor_t, similar to the read prefs and read concern it has already. Whenever a cursor is created, if and only if it's created by one of the command helpers whose names end in _with_write_concern, set the cursor's write concern. Don't append it to the command document yet.
Then, when the user calls mongoc_cursor_next it calls _mongoc_cursor_cursorid_refresh_from_command which calls _mongoc_cursor_run_command. In there we get a server stream and know its wire version for certain. If it's 5+ and cursor->write_concern is not NULL or the default, append the write concern to the command before the call to mongoc_cluster_run_command_monitored.