MONGOC_COLLECTION_INSERT_ONE(3)
π mongoc_collection_insert_one β the libmongoc era π
okay so bestie, this is the MONGOC_COLLECTION_INSERT_ONE(3) page from libmongoc, and it's literally the "put one (1) document into a collection" function. one doc. singular. no cap. if you've got a whole array to shove in, she's gonna point you elsewhere (spoiler: mongoc_collection_insert_many()). let's get into it π«Ά
β¨ SYNOPSIS β¨
bool
mongoc_collection_insert_one (mongoc_collection_t *collection,
const bson_t *document,
const bson_t *opts,
bson_t *reply,
bson_error_t *error);
look at her. five args. iconic signature, no notes π
πΈ PARAMETERS πΈ
- collection: a
mongoc_collection_t. the collection. the destination. main character energy. - document: a
bson_t. the one (1) doc going in π - reply: a maybe-
NULLpointer to overwritable storage for abson_tthat's gonna hold the results. - error: an optional location for a
bson_error_t, orNULLif you're built different and don't want to know.
and then optsβ¦ opts may be NULL, or it can be a BSON document with extra command options. here's the whole menu, don't skip a single item, i'm watching π:
writeConcernβ construct amongoc_write_concern_tand usemongoc_write_concern_append()to add the write concern toopts. there's example code over onmongoc_client_write_command_with_opts()if you need the vibe βοΈsessionIdβ first, construct amongoc_client_session_twithmongoc_client_start_session(). you can begin a transaction withmongoc_client_session_start_transaction(), optionally with amongoc_transaction_opt_tthat overrides the options inherited fromcollection, and usemongoc_client_session_append()to add the session toopts. again, example code lives atmongoc_client_session_t. go look, i'll wait π§validateβ construct a bitwise-or of all thebson_validate_flags_tyou desire. set it tofalseto skip client-side validation of the provided BSON documents. bold move but ok πcommentβ abson_value_tspecifying the comment to attach to this command. the comment will appear in log messages, profiler output, andcurrentOpoutput. requires MongoDB 4.4 or later βbypassDocumentValidationβ set totrueto skip server-side schema validation of the provided BSON documents. living dangerously, love that for you π₯
π¦ DESCRIPTION π¦
this function shall insert document into collection. that's it. that's the whole job. slay.
to insert an array of documents, see mongoc_collection_insert_many(). she does bulk, this one does one.
if no _id element is found in document, then a bson_oid_t will be generated locally and added to the document. so like, she'll give your doc an id if it forgot its own. so thoughtful π
if you pass a non-NULL reply, it is filled out with an "insertedCount" field. and if the document was successfully inserted, reply will also contain an "insertedId" field containing the inserted document's _id. but if there's a server error, then reply contains either a "writeErrors" array with one subdocument or a "writeConcernErrors" array.
β οΈ bestie thereplymust be freed withbson_destroy(). do NOT forget. that's a leak and leaks are not cute π
π ERRORS π
errors are propagated via the error parameter. that's the whole section. short and honest.
π RETURNS π
returns true if successful. returns false and sets error if there are invalid arguments or a server or network error.
a write concern timeout or write concern error is considered a failure. yes, even the write concern ones. fr fr.
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
and that's the page!! mwah π β brought to you by libmongoc version 2.5.6, dated Oct 09, 2026. go forth and insert exactly one (1) document, bestie. periodt πΈπ«Άβ¨
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms