Skip to content

CDRIVER-1986 remove deprecated mongoc_collection_save #1911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Unreleased (2.0.0)
* `mongoc_server_description_ismaster` is removed. Use the equivalent `mongoc_server_description_hello` instead.
* `MONGOC_QUERY_SLAVE_OK` is removed. Use the equivalent `MONGOC_QUERY_SECONDARY_OK` instead.
* `MONGOC_URI_SLAVEOK` is removed. It was unused.
* `mongoc_collection_save` is removed. Use `mongoc_collection_insert_one` or `mongoc_collection_replace_one` instead.

### Forwarding headers (`#include <bson.h>` and `#include <mongoc.h>`)

Expand Down
46 changes: 0 additions & 46 deletions src/libmongoc/doc/mongoc_collection_save.rst

This file was deleted.

1 change: 0 additions & 1 deletion src/libmongoc/doc/mongoc_collection_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Read preferences and write concerns are inherited from the parent client. They c
mongoc_collection_rename
mongoc_collection_rename_with_opts
mongoc_collection_replace_one
mongoc_collection_save
mongoc_collection_set_read_concern
mongoc_collection_set_read_prefs
mongoc_collection_set_write_concern
Expand Down
60 changes: 0 additions & 60 deletions src/libmongoc/src/mongoc/mongoc-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2297,66 +2297,6 @@ mongoc_collection_replace_one (mongoc_collection_t *collection,
}


/*
*--------------------------------------------------------------------------
*
* mongoc_collection_save --
*
* Save @document to @collection.
*
* If the document has an _id field, it will be updated. Otherwise,
* the document will be inserted into the collection.
*
* Returns:
* true if successful; otherwise false and @error is set.
*
* Side effects:
* @error is set upon failure if non-NULL.
*
*--------------------------------------------------------------------------
*/

bool
mongoc_collection_save (mongoc_collection_t *collection,
const bson_t *document,
const mongoc_write_concern_t *write_concern,
bson_error_t *error)
{
bson_iter_t iter;
bool ret;
bson_t selector;

BSON_ASSERT_PARAM (collection);
BSON_ASSERT_PARAM (document);

BEGIN_IGNORE_DEPRECATIONS
if (!bson_iter_init_find (&iter, document, "_id")) {
return mongoc_collection_insert (collection, MONGOC_INSERT_NONE, document, write_concern, error);
}

bson_init (&selector);
if (!bson_append_iter (&selector, NULL, 0, &iter)) {
_mongoc_set_error (
error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "Failed to append bson to create update.");
bson_destroy (&selector);
return false;
}

/* this document will be inserted, validate same as for inserts */
if (!_mongoc_validate_new_document (document, _mongoc_default_insert_vflags, error)) {
return false;
}

ret = mongoc_collection_update (
collection, MONGOC_UPDATE_UPSERT | MONGOC_UPDATE_NO_VALIDATE, &selector, document, write_concern, error);
END_IGNORE_DEPRECATIONS

bson_destroy (&selector);

return ret;
}


bool
mongoc_collection_remove (mongoc_collection_t *collection,
mongoc_remove_flags_t flags,
Expand Down
6 changes: 0 additions & 6 deletions src/libmongoc/src/mongoc/mongoc-collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,6 @@ MONGOC_EXPORT (bool) mongoc_collection_delete (mongoc_collection_t *collection,
const mongoc_write_concern_t *write_concern,
bson_error_t *error);

BSON_DEPRECATED_FOR (mongoc_collection_insert_one or mongoc_collection_replace_one)
MONGOC_EXPORT (bool) mongoc_collection_save (mongoc_collection_t *collection,
const bson_t *document,
const mongoc_write_concern_t *write_concern,
bson_error_t *error);

MONGOC_EXPORT (bool)
mongoc_collection_remove (mongoc_collection_t *collection,
mongoc_remove_flags_t flags,
Expand Down
54 changes: 0 additions & 54 deletions src/libmongoc/tests/test-mongoc-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,59 +854,6 @@ test_insert_command_keys (void)
}


static void
test_save (void)
{
mongoc_collection_t *collection;
mongoc_database_t *database;
mongoc_client_t *client;
bson_context_t *context;
bson_error_t error;
bson_oid_t oid;
unsigned i;
bson_t b;
bool r;

client = test_framework_new_default_client ();
ASSERT (client);

database = get_test_database (client);
ASSERT (database);

collection = get_test_collection (client, "test_save");
ASSERT (collection);

/* don't care if ns not found. */
(void) mongoc_collection_drop (collection, &error);

context = bson_context_new (BSON_CONTEXT_NONE);
ASSERT (context);

BEGIN_IGNORE_DEPRECATIONS

for (i = 0; i < 10; i++) {
bson_init (&b);
bson_oid_init (&oid, context);
bson_append_oid (&b, "_id", 3, &oid);
bson_append_utf8 (&b, "hello", 5, "/world", 5);
ASSERT_OR_PRINT (mongoc_collection_save (collection, &b, NULL, &error), error);
bson_destroy (&b);
}

r = mongoc_collection_save (collection, tmp_bson ("{'': 1}"), NULL, &error);

END_IGNORE_DEPRECATIONS

ASSERT (!r);
ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "invalid document");

mongoc_collection_destroy (collection);
mongoc_database_destroy (database);
bson_context_destroy (context);
mongoc_client_destroy (client);
}


static void
test_regex (void)
{
Expand Down Expand Up @@ -5810,7 +5757,6 @@ test_collection_install (TestSuite *suite)
TestSuite_AddFull (
suite, "/Collection/insert/oversize", test_insert_oversize, NULL, NULL, test_framework_skip_if_slow_or_live);
TestSuite_AddMockServerTest (suite, "/Collection/insert/keys", test_insert_command_keys);
TestSuite_AddLive (suite, "/Collection/save", test_save);
TestSuite_AddLive (suite, "/Collection/insert/w0", test_insert_w0);
TestSuite_AddLive (suite, "/Collection/update/w0", test_update_w0);
TestSuite_AddLive (suite, "/Collection/remove/w0", test_remove_w0);
Expand Down