How to add multiple objects to array based on key?
I need to add multiple objects in one query, check if each object key
doesn't exist or duplicate, else add object. (label
can be duplicate)
Schema
new Schema({
additional: [
{
key: { type: String, required: true, unique: true },
label: { type: String, required: true }
}
]
})
request payload:
[ {key: "city", label: "CITY"}, {key: "gender", label: "GENDER"}
, {key: "city" ,label: "CITY1}, {key: "city2", label: "CITY"}]
Expected results:
[
{key: "city", label: "CITY"},
{key: "gender", label: "GENDER"},
{key: "city2", label: "CITY"}
]
I tried to find solutions but couldn't find any.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…