Skip to content

Commit

Permalink
materialize-mongodb: remove _id property from loaded docs
Browse files Browse the repository at this point in the history
Removes the `_id` property before returning loaded documents. This is to
prevent that property from causing schema validation errors for collections
that use a top level `reduce: { strategy: merge }`.
  • Loading branch information
psFried committed Jan 3, 2024
1 parent 3074b78 commit f07b4e4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion materialize-mongodb/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,22 @@ func (t *transactor) storeWorker(
}

func sanitizeDocument(doc map[string]interface{}) map[string]interface{} {
// We need to remove the _id property from loaded documents because the
// Flow collection schemas may forbid that property. If we left it in, it
// could cause validation errors on loaded documents.
delete(doc, idField)
return sanitizeDocumentInner(doc)
}

func sanitizeDocumentInner(doc map[string]interface{}) map[string]interface{} {
for key, value := range doc {
switch v := value.(type) {
case float64:
if math.IsNaN(v) {
doc[key] = "NaN"
}
case map[string]interface{}:
doc[key] = sanitizeDocument(v)
doc[key] = sanitizeDocumentInner(v)
}
}

Expand Down

0 comments on commit f07b4e4

Please sign in to comment.