If you have imported data using a CSV file containing one device column and more than one variable column, your data
collection in MongoDB may contain some errors : some of the documents may contain duplicate elements in their
provenance.provWasAssociatedWith
field. To detect potential errors, we advise to run this MongoDB command on your
database :
db.data.count({
"provenance.provWasAssociatedWith.1": {
$exists: true
},
$where: function () {
const s = new Set(this.provenance.provWasAssociatedWith.map(el => el.uri));
return s.size < this.provenance.provWasAssociatedWith.length;
}
})
If the result of the previous command is more than 0, you should run the next one to fix the incorrect data entries.
db.data.updateMany({
"provenance.provWasAssociatedWith.1": {$exists: true},
$where: function () {
const s = new Set(this.provenance.provWasAssociatedWith.map((el) => el.uri));
return s.size < this.provenance.provWasAssociatedWith.length;
}
}, [
{$addFields: {"provenance.provWasAssociatedWith": {"$setUnion": ["$provenance.provWasAssociatedWith", []]}}}
]
)
This command will update the provenance.provWasAssociatedWith
fields to remove duplicate elements. To run it, your
MongoDB server will need to be at least on the 4.2 version.