* UpsertDocument performs an insert when an update did not match any rows * and returns the inserted object ID. */
| 3895 | * and returns the inserted object ID. |
| 3896 | */ |
| 3897 | static pgbson * |
| 3898 | UpsertDocument(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 | /* |
no test coverage detected