MCPcopy Create free account
hub / github.com/documentdb/documentdb / UpsertDocument

Function UpsertDocument

pg_documentdb/src/commands/update.c:3897–3928  ·  view source on GitHub ↗

* UpsertDocument performs an insert when an update did not match any rows * and returns the inserted object ID. */

Source from the content-addressed store, hash-verified

3895 * and returns the inserted object ID.
3896 */
3897static pgbson *
3898UpsertDocument(MongoCollection *collection, const bson_value_t *update,
3899 const bson_value_t *query, const bson_value_t *arrayFilters,
3900 ExprEvalState *stateForSchemaValidation,
3901 bool hasOnlyObjectIdFilter, const bson_value_t *variableSpec)
3902{
3903 pgbson *emptyDocument = PgbsonInitEmpty();
3904
3905 /*
3906 * Perform regular bson update document because this is an upsert case without
3907 * update change tracking
3908 */
3909 pgbson *newDoc = BsonUpdateDocument(emptyDocument, update, query, arrayFilters,
3910 variableSpec);
3911
3912 int64 newShardKeyHash =
3913 ComputeShardKeyHashForDocument(collection->shardKey, collection->collectionId,
3914 newDoc);
3915
3916 pgbson *objectId = PgbsonGetDocumentId(newDoc);
3917
3918 if (EnableSchemaValidation && stateForSchemaValidation != NULL)
3919 {
3920 bson_value_t newDocValue = ConvertPgbsonToBsonValue(newDoc);
3921 ValidateSchemaOnDocumentInsert(stateForSchemaValidation, &newDocValue,
3922 FAILED_VALIDATION_ERROR_MSG);
3923 }
3924
3925 DoInsertForUpdate(collection, newShardKeyHash, objectId, newDoc,
3926 hasOnlyObjectIdFilter, update);
3927 return objectId;
3928}
3929
3930
3931/*

Callers 1

ProcessUpdateFunction · 0.85

Calls 7

PgbsonInitEmptyFunction · 0.85
BsonUpdateDocumentFunction · 0.85
PgbsonGetDocumentIdFunction · 0.85
ConvertPgbsonToBsonValueFunction · 0.85
DoInsertForUpdateFunction · 0.85

Tested by

no test coverage detected