MONGOC_COLLECTION_SAVE(3)

πŸ’– MONGOC_COLLECTION_SAVE πŸ’–

okay so bestie, we're in libmongoc land and the page is literally called MONGOC_COLLECTION_SAVE(3). yes it's a whole manual page, no it's not scary, and yes i'm gonna walk you through it like we're texting at 2am 🫢


🚨 WARNING 🚨

Deprecated since version 1.9.0: Use mongoc_collection_insert_one() or mongoc_collection_replace_one() with "upsert" instead.

not me having to tell you this function is lowkey retired πŸ’€ it's giving "still works but the group chat has moved on". use the newer ones, i'm begging.


✨ SYNOPSIS ✨

bool
mongoc_collection_save (mongoc_collection_t *collection,
                        const bson_t *document,
                        const mongoc_write_concern_t *write_concern,
                        bson_error_t *error);

look at that signature, it's iconic, it's neat, it's literally right there πŸ‘‘


πŸŽ€ PARAMETERS πŸŽ€

  • collection β€” a mongoc_collection_t. that's the collection we're saving into, the main character of the operation ngl.
  • document β€” a bson_t holding the document. this is the actual content, the vibe, the payload.
  • write_concern β€” a mongoc_write_concern_t, or NULL if you want the default write concern. pass NULL and let the default handle it, no thoughts head empty, it's fine 🌸
  • error β€” an optional spot to put a bson_error_t, or NULL. optional means optional, so if you don't care about the tea, pass NULL πŸ™ˆ

🌈 DESCRIPTION 🌈

so this function shall save a document into collection. and here's the lil plot twist that makes it save and not just insert:

  • if the document has an _id field, it will be updated ✨
  • otherwise, it will be inserted πŸ¦‹

that's it. that's the whole trick. update-if-it-has-an-id, insert-if-it-doesn't. periodt πŸ’…


😭 ERRORS 😭

errors get propagated through the error parameter. so if something goes sideways, that's where you'll find out what happened, bestie.


⭐ RETURNS ⭐

returns true if it worked. slay.

returns false and sets error if:

  • there are invalid arguments, or
  • there's a server error or a network error.
heads up, this one's important: a write concern timeout or a write concern error counts as a failure. so like, false energy, not true energy. don't get it twisted πŸ’€

πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc


2009-present, MongoDB, Inc.


and that's the whole page, bestie πŸ’– you ate. this was libmongoc version 1.30.13, dated Oct 09, 2026, and the page itself is MONGOC_COLLECTION_SAVE(3). go forth and use mongoc_collection_insert_one() or mongoc_collection_replace_one() with "upsert" instead, fr fr 🌸✨🫢

πŸ“¦ where this page comes from