* UpdateOneInternal updates a single document with a specific shard key value filter. * Returns whether a document was updated and if so sets the updatedDocument and * whether reinsertion will be required (due to shard key value change). */
| 3199 | * whether reinsertion will be required (due to shard key value change). |
| 3200 | */ |
| 3201 | static void |
| 3202 | UpdateOneInternal(MongoCollection *collection, UpdateOneParams *updateOneParams, |
| 3203 | int64 shardKeyHash, UpdateOneResult *result, |
| 3204 | ExprEvalState *stateForSchemaValidation) |
| 3205 | { |
| 3206 | /* initialize result */ |
| 3207 | memset(result, 0, sizeof(UpdateOneResult)); |
| 3208 | |
| 3209 | UpdateCandidate updateCandidate = { 0 }; |
| 3210 | |
| 3211 | bool getExistingDoc = updateOneParams->returnDocument != UPDATE_RETURNS_NONE || |
| 3212 | updateOneParams->returnFields != NULL || |
| 3213 | NeedExistingDocForValidation(stateForSchemaValidation, |
| 3214 | collection); |
| 3215 | bool hasOnlyObjectIdFilter = false; |
| 3216 | bool foundDocument = SelectUpdateCandidate(collection, |
| 3217 | shardKeyHash, |
| 3218 | updateOneParams, |
| 3219 | &updateCandidate, |
| 3220 | getExistingDoc, |
| 3221 | &hasOnlyObjectIdFilter); |
| 3222 | |
| 3223 | if (!foundDocument) |
| 3224 | { |
| 3225 | /* no documents matched the query */ |
| 3226 | |
| 3227 | if (updateOneParams->isUpsert) |
| 3228 | { |
| 3229 | pgbson *emptyDocument = PgbsonInitEmpty(); |
| 3230 | |
| 3231 | /* |
| 3232 | * Perform regular bson update document because this is an upsert case without |
| 3233 | * update change tracking |
| 3234 | */ |
| 3235 | pgbson *newDoc = BsonUpdateDocument(emptyDocument, updateOneParams->update, |
| 3236 | updateOneParams->query, |
| 3237 | updateOneParams->arrayFilters, |
| 3238 | updateOneParams->variableSpec); |
| 3239 | |
| 3240 | pgbson *objectId = PgbsonGetDocumentId(newDoc); |
| 3241 | |
| 3242 | int64 newShardKeyHash = |
| 3243 | ComputeShardKeyHashForDocument(collection->shardKey, |
| 3244 | collection->collectionId, newDoc); |
| 3245 | |
| 3246 | if (EnableSchemaValidation && stateForSchemaValidation != NULL) |
| 3247 | { |
| 3248 | bson_value_t newDocValue = ConvertPgbsonToBsonValue(newDoc); |
| 3249 | ValidateSchemaOnDocumentInsert(stateForSchemaValidation, &newDocValue, |
| 3250 | FAILED_VALIDATION_ERROR_MSG); |
| 3251 | } |
| 3252 | |
| 3253 | if (newShardKeyHash == shardKeyHash) |
| 3254 | { |
| 3255 | /* shard key unchanged, upsert now */ |
| 3256 | DoInsertForUpdate(collection, newShardKeyHash, objectId, newDoc, |
| 3257 | hasOnlyObjectIdFilter, updateOneParams->update); |
| 3258 | result->reinsertDocument = NULL; |
no test coverage detected