Combine arrays when importing with mongoimport?

When importing my a json file using mongoimport, arrays are overwritten due to the field name for the actual array being the same, regardless of the elements in the array containing different data.

Is there a way to essentially combine and merge the arrays, instead of overwriting the array, when importing with mongoimport?

Hi @James_Anderson,

Welcome to MongoDB community!

Although mongoimport can merge documents based on a list of fields into your collection it cannot perform complex transformation like concatenating arrays:

https://docs.mongodb.com/database-tools/mongoimport/#replace-matching-documents-during-import

What I would suggest is to load the documents into a temporary stage collection and perform a post command/script doing a $merge aggregation with $zip of the source and target arrays.

Let me know if this is acceptable.

Best
Pavel

Hi @James_Anderson, although mongoimport can’t do this currently, I think this is a pretty good suggestion. I made a feature request for this on Jira: TOOLS-2765. The feature might not get added very fast - we have a large backlog - but you can follow that ticket to be notified of updates.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.