【发布时间】:2021-06-20 20:40:40
【问题描述】:
我的 MongoDB 集合如下:
[
{ x: "a", t: 1, s: 'old' },
{ x: "a", t: 3, s: 'old' },
{ x: "b", t: 1, s: 'old' },
{ x: "b", t: 4, s: 'old' }
]
我还有一个数组如下:
const list = [
{ x: "a", t: 1, s: 'new' },
{ x: "a", t: 2, s: 'new' },
{ x: "b", t: 2, s: 'new' },
{ x: "b", t: 3, s: 'new' },
]
现在我要插入
列表
将数组放入 mongo 集合中:
- 如果要插入的项目存在于集合中,则不要插入它。
- 如果要插入的项目存在于集合中,则插入它。
最终结果应该如下:
[
{ x: "a", t: 1, s: 'old' },
{ x: "a", t: 2, s: 'new' },
{ x: "a", t: 3, s: 'old' },
{ x: "b", t: 1, s: 'old' },
{ x: "b", t: 2, s: 'new' },
{ x: "b", t: 3, s: 'new' },
{ x: "b", t: 4, s: 'old' }
]
【问题讨论】:
标签: mongodb mongoose aggregation-framework