如何根据键将多个对象添加到数组?
我需要在一个查询中添加多个对象,检查每个对象object key
是否不存在或重复,否则添加对象。(label
可以重复)
架构
new Schema({
additional: [
{
key: { type: String, required: true, unique: true },
label: { type: String, required: true }
}
]
})
请求有效载荷:
[ {key: "city", label: "CITY"}, {key: "gender", label: "GENDER"}
, {key: "city" ,label: "CITY1}, {key: "city2", label: "CITY"}]
预期成绩:
[
{key: "city", label: "CITY"},
{key: "gender", label: "GENDER"},
{key: "city2", label: "CITY"}
]
我试图找到解决方案,但找不到任何解决方案。