Answer the question
In order to leave comments, you need to log in
How to merge Mongo arrays by unique key?
Given:
1. Array addTags = [ 'tag1', 'tag2', ... ]
2. Simple selection like condition = {param:"value"}
3. Object like this:
someObject {
...
param: String,
tags:[{
name:String,
timestamp:Date
}]
}
const tagDates = addTags.map(tag=>{name:tag, timestamp:Date.now()}
findMany(condition).then(
foundObjects=>foundObjects.forEach(object=>{
const newTags = mergeByName(object.tags, tagDates)
updateOne({id:object.id},{tags:newTags}).then()
})
)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question