MONGOC_COLLECTION_INSERT(3)

πŸ’… MONGOC_COLLECTION_INSERT(3) β€” the libmongoc girlies' guide πŸŽ€

okay so bestie, welcome to the libmongoc channel of the group chat πŸ’– this one's about mongoc_collection_insert, and honestly? she's a classic. no cap.


✨ SYNOPSIS ✨

like, here's literally the whole vibe signature:

bool
mongoc_collection_insert (mongoc_collection_t *collection,
                          mongoc_insert_flags_t flags,
                          const bson_t *document,
                          const mongoc_write_concern_t *write_concern,
                          bson_error_t *error);

look at her. five args. main character energy fr fr 🌟


🌸 PARAMETERS 🌸

  • collection: a mongoc_collection_t. this is the collection you're inserting into, obvi 🫢
  • flags: a mongoc_insert_flags_t. the flags, bestie 🚩
  • document: a bson_t. the actual doc going in. the moment. the icon πŸ‘‘
  • write_concern: a mongoc_write_concern_t. how much you care about it actually landing πŸ’…
  • error: an optional location for a bson_error_t, or NULL. optional! she can be NULL! πŸ™ˆ

πŸ¦‹ DESCRIPTION πŸ¦‹

NOTE: πŸ’€ if you wanna pass additional options, use mongoc_collection_insert_one() or mongoc_collection_insert_many(). that's the upgraded version, iykyk. this one here is the classic model.

this function shall insert document into collection. that's it, that's the whole job. slay. ✨

okay but here's the part you need to know: if no _id element is found in document, then a bson_oid_t will be generated locally and added to the document. yes bestie, locally, on your machine, not on the server 🌈 so if you must know the inserted document's _id, generate it in your own code and include it in the document. be the main character of your own _id πŸ’–

and the _id you generate can be a bson_oid_t or any other non-array BSON type. like, any non-array type. go off, honestly. just not an array. arrays are not invited to this party 🚫


πŸ’€ ERRORS πŸ’€

errors are propagated via the error parameter. that's the whole section. short and iconic. πŸŽ€


⭐ RETURNS ⭐

returns true if successful. iconic, we love to see it πŸ’œ

returns false and sets error if there are invalid arguments, or a server error, or a network error. not the vibe, but she's honest about it 😭

and just so you know: a write concern timeout or a write concern error is considered a failure. yes, even the timeout. even the write concern error. failure. periodt. πŸ”₯

🌈 AUTHOR 🌈

MongoDB, Inc β€” the og authors fr fr πŸ‘‘


2009-present, MongoDB, Inc. β€” they've been holding it down since 2009, that's a long run bestie πŸ“


aaand that's the page!! 🫢 version 2.5.6, dated Oct 09, 2026, from the libmongoc girlies themselves β€” go insert something cute πŸ’…βœ¨

πŸ“¦ where this page comes from