Skip to content

Commit 3b5d064

Browse files
authored
CDRIVER-3054 Revise docs from mongoc_collection_(remove|insert|update) (#1783)
* recommend `mongoc_collection_remove` as an alternative to `mongoc_collection_delete` * replace "Superseded by" to avoid suggesting functions are obsolete. * remove internal comments redundant with public docs. * simplify `test_update` and test a replacement document can be passed * add missing `mongoc_init` and `mongoc_cleanup`
1 parent 1bafa94 commit 3b5d064

File tree

7 files changed

+61
-131
lines changed

7 files changed

+61
-131
lines changed

src/libmongoc/doc/mongoc_collection_delete.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mongoc_collection_delete()
66
.. warning::
77
.. deprecated:: 1.9.0
88

9-
Use :symbol:`mongoc_collection_delete_one()` or :symbol:`mongoc_collection_delete_many()` instead.
9+
Use :symbol:`mongoc_collection_remove`, :symbol:`mongoc_collection_delete_one()` or :symbol:`mongoc_collection_delete_many()` instead.
1010

1111
Synopsis
1212
--------

src/libmongoc/doc/mongoc_collection_insert.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Parameters
2727
Description
2828
-----------
2929

30-
Superseded by :symbol:`mongoc_collection_insert_one()` and :symbol:`mongoc_collection_insert_many()`.
30+
.. note::
31+
32+
To pass additional options, use :symbol:`mongoc_collection_insert_one()` or :symbol:`mongoc_collection_insert_many()`.
3133

3234
This function shall insert ``document`` into ``collection``.
3335

src/libmongoc/doc/mongoc_collection_remove.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Parameters
2727
Description
2828
-----------
2929

30-
Superseded by :symbol:`mongoc_collection_delete_one` and :symbol:`mongoc_collection_delete_many`.
30+
.. note::
31+
32+
To pass additional options, use :symbol:`mongoc_collection_delete_one` or :symbol:`mongoc_collection_delete_many`.
3133

3234
This function shall remove documents in the given ``collection`` that match ``selector``. The bson ``selector`` is not validated, simply passed along as appropriate to the server. As such, compatibility and errors should be validated in the appropriate server documentation.
3335

src/libmongoc/doc/mongoc_collection_update.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Parameters
2929
Description
3030
-----------
3131

32-
Superseded by :symbol:`mongoc_collection_update_one`, :symbol:`mongoc_collection_update_many`, and :symbol:`mongoc_collection_replace_one`.
32+
.. note::
33+
34+
To pass additional options, use :symbol:`mongoc_collection_update_one`, :symbol:`mongoc_collection_update_many`, or :symbol:`mongoc_collection_replace_one`.
3335

3436
This function shall update documents in ``collection`` that match ``selector``.
3537

src/libmongoc/examples/example-update.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
int
44
main (void)
55
{
6+
mongoc_init ();
67
bson_t *to_insert = BCON_NEW ("_id", BCON_INT32 (1));
78
bson_t *selector = BCON_NEW ("_id", "{", "$gt", BCON_INT32 (0), "}");
89
bson_t *update = BCON_NEW ("$set", "{", "x", BCON_INT32 (1), "}");
@@ -63,6 +64,6 @@ main (void)
6364
mongoc_collection_destroy (coll);
6465
mongoc_uri_destroy (uri);
6566
mongoc_client_destroy (client);
66-
67+
mongoc_cleanup ();
6768
return EXIT_SUCCESS;
6869
}

src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,31 +1935,6 @@ mongoc_collection_insert_many (mongoc_collection_t *collection,
19351935
RETURN (ret);
19361936
}
19371937

1938-
/*
1939-
*--------------------------------------------------------------------------
1940-
*
1941-
* mongoc_collection_update --
1942-
*
1943-
* Updates one or more documents matching @selector with @update.
1944-
*
1945-
* Parameters:
1946-
* @collection: A mongoc_collection_t.
1947-
* @flags: The flags for the update.
1948-
* @selector: A bson_t containing your selector.
1949-
* @update: A bson_t containing your update document.
1950-
* @write_concern: The write concern or NULL.
1951-
* @error: A location for an error or NULL.
1952-
*
1953-
* Returns:
1954-
* true if successful; otherwise false and @error is set.
1955-
*
1956-
* Side effects:
1957-
* @collection->gle is setup, depending on write_concern->w value.
1958-
* @error is setup upon failure.
1959-
*
1960-
*--------------------------------------------------------------------------
1961-
*/
1962-
19631938
bool
19641939
mongoc_collection_update (mongoc_collection_t *collection,
19651940
mongoc_update_flags_t uflags,
@@ -2363,38 +2338,6 @@ mongoc_collection_save (mongoc_collection_t *collection,
23632338
}
23642339

23652340

2366-
/*
2367-
*--------------------------------------------------------------------------
2368-
*
2369-
* mongoc_collection_remove --
2370-
*
2371-
* Delete one or more items from a collection. If you want to
2372-
* limit to a single delete, provided MONGOC_REMOVE_SINGLE_REMOVE
2373-
* for @flags.
2374-
*
2375-
* Superseded by mongoc_collection_delete_one/many.
2376-
*
2377-
* Parameters:
2378-
* @collection: A mongoc_collection_t.
2379-
* @flags: the delete flags or 0.
2380-
* @selector: A selector of documents to delete.
2381-
* @write_concern: A write concern or NULL. If NULL, the default
2382-
* write concern for the collection will be used.
2383-
* @error: A location for an error or NULL.
2384-
*
2385-
* Returns:
2386-
* true if successful; otherwise false and error is set.
2387-
*
2388-
* If the write concern does not dictate checking the result, this
2389-
* function may return true even if it failed.
2390-
*
2391-
* Side effects:
2392-
* @collection->gle is setup, depending on write_concern->w value.
2393-
* @error is setup upon failure.
2394-
*
2395-
*--------------------------------------------------------------------------
2396-
*/
2397-
23982341
bool
23992342
mongoc_collection_remove (mongoc_collection_t *collection,
24002343
mongoc_remove_flags_t flags,

src/libmongoc/tests/test-mongoc-collection.c

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,83 +1023,63 @@ test_decimal128 (void *ctx)
10231023
static void
10241024
test_update (void)
10251025
{
1026-
mongoc_collection_t *collection;
1027-
mongoc_database_t *database;
1028-
mongoc_client_t *client;
1029-
bson_context_t *context;
10301026
bson_error_t error;
1031-
bool r;
1032-
bson_oid_t oid;
1033-
unsigned i;
1034-
bson_t b;
1035-
bson_t q;
1036-
bson_t u;
1037-
bson_t set;
1038-
1039-
client = test_framework_new_default_client ();
1040-
ASSERT (client);
1041-
1042-
database = get_test_database (client);
1043-
ASSERT (database);
1044-
1045-
collection = get_test_collection (client, "test_update");
1046-
ASSERT (collection);
1047-
1048-
context = bson_context_new (BSON_CONTEXT_NONE);
1049-
ASSERT (context);
1050-
1051-
for (i = 0; i < 10; i++) {
1052-
bson_init (&b);
1053-
bson_oid_init (&oid, context);
1054-
bson_append_oid (&b, "_id", 3, &oid);
1055-
bson_append_utf8 (&b, "utf8", 4, "utf8 string", 11);
1056-
bson_append_int32 (&b, "int32", 5, 1234);
1057-
bson_append_int64 (&b, "int64", 5, 12345678);
1058-
bson_append_bool (&b, "bool", 4, 1);
1059-
1060-
ASSERT_OR_PRINT (mongoc_collection_insert_one (collection, &b, NULL, NULL, &error), error);
1061-
1062-
bson_init (&q);
1063-
bson_append_oid (&q, "_id", 3, &oid);
10641027

1065-
bson_init (&u);
1066-
bson_append_document_begin (&u, "$set", 4, &set);
1067-
bson_append_utf8 (&set, "utf8", 4, "updated", 7);
1068-
bson_append_document_end (&u, &set);
1069-
1070-
ASSERT_OR_PRINT (mongoc_collection_update (collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error), error);
1028+
mongoc_client_t *client = test_framework_new_default_client ();
1029+
mongoc_collection_t *coll = get_test_collection (client, "test_update");
10711030

1072-
bson_destroy (&b);
1073-
bson_destroy (&q);
1074-
bson_destroy (&u);
1031+
// Test a successful update:
1032+
{
1033+
mongoc_collection_drop (coll, NULL);
1034+
bson_t *b = tmp_bson ("{'foo' : 'bar'}");
1035+
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, b, NULL, NULL, &error), error);
1036+
1037+
bson_t *q = tmp_bson ("{}");
1038+
bson_t *u = tmp_bson ("{'$set': {'foo': 'updated' }}");
1039+
ASSERT_OR_PRINT (mongoc_collection_update (coll, MONGOC_UPDATE_NONE, q, u, NULL, &error), error);
1040+
1041+
bson_t *f = tmp_bson ("{'foo': 'updated'}");
1042+
int64_t count = mongoc_collection_count_documents (coll, f, NULL, NULL, NULL, &error);
1043+
ASSERT_OR_PRINT (count >= 0, error);
1044+
ASSERT_CMPINT64 (count, ==, 1);
10751045
}
10761046

1077-
bson_init (&q);
1078-
bson_init (&u);
1079-
BSON_APPEND_INT32 (&u, "abcd", 1);
1080-
BSON_APPEND_INT32 (&u, "$hi", 1);
1081-
r = mongoc_collection_update (collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error);
1082-
ASSERT (!r);
1083-
ASSERT (error.domain == MONGOC_ERROR_COMMAND);
1084-
ASSERT (error.code == MONGOC_ERROR_COMMAND_INVALID_ARG);
1085-
bson_destroy (&q);
1086-
bson_destroy (&u);
1047+
// Test an invalid update document with both $-prefixed and non-$-prefixed fields:
1048+
{
1049+
bson_t *q = tmp_bson ("{}");
1050+
bson_t *u = tmp_bson ("{'abcd': 1, '$hi': 1 }");
1051+
bool ok = mongoc_collection_update (coll, MONGOC_UPDATE_NONE, q, u, NULL, &error);
1052+
ASSERT (!ok);
1053+
ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "Invalid key");
1054+
}
10871055

1088-
bson_init (&q);
1089-
bson_init (&u);
1090-
BSON_APPEND_INT32 (&u, "" /* empty key */, 1);
1091-
r = mongoc_collection_update (collection, MONGOC_UPDATE_NONE, &q, &u, NULL, &error);
1092-
ASSERT (!r);
1093-
ASSERT (error.domain == MONGOC_ERROR_COMMAND);
1094-
ASSERT (error.code == MONGOC_ERROR_COMMAND_INVALID_ARG);
1095-
bson_destroy (&q);
1096-
bson_destroy (&u);
1056+
// Test an invalid update document with an empty field:
1057+
{
1058+
bson_t *q = tmp_bson ("{}");
1059+
bson_t *u = tmp_bson ("{'': 1 }");
1060+
bool ok = mongoc_collection_update (coll, MONGOC_UPDATE_NONE, q, u, NULL, &error);
1061+
ASSERT (!ok);
1062+
ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "empty key");
1063+
}
10971064

1098-
ASSERT_OR_PRINT (mongoc_collection_drop (collection, &error), error);
1065+
// Test a successful replacement:
1066+
{
1067+
mongoc_collection_drop (coll, NULL);
1068+
bson_t *b = tmp_bson ("{'foo' : 'bar'}");
1069+
ASSERT_OR_PRINT (mongoc_collection_insert_one (coll, b, NULL, NULL, &error), error);
1070+
1071+
bson_t *q = tmp_bson ("{}");
1072+
bson_t *u = tmp_bson ("{'foo2': 'bar2'}");
1073+
ASSERT_OR_PRINT (mongoc_collection_update (coll, MONGOC_UPDATE_NONE, q, u, NULL, &error), error);
1074+
1075+
bson_t *f = tmp_bson ("{'foo2': 'bar2'}");
1076+
int64_t count = mongoc_collection_count_documents (coll, f, NULL, NULL, NULL, &error);
1077+
ASSERT_OR_PRINT (count >= 0, error);
1078+
ASSERT_CMPINT64 (count, ==, 1);
1079+
ASSERT_OR_PRINT (mongoc_collection_drop (coll, &error), error);
1080+
}
10991081

1100-
mongoc_collection_destroy (collection);
1101-
mongoc_database_destroy (database);
1102-
bson_context_destroy (context);
1082+
mongoc_collection_destroy (coll);
11031083
mongoc_client_destroy (client);
11041084
}
11051085

0 commit comments

Comments
 (0)