【发布时间】:2016-01-04 04:27:28
【问题描述】:
我在 Meteor 中使用 MongoDB。我发现更新(如果已经存在)数据或插入集合update() 可以与upsert: true 一起使用。还有一个名为upsert() 的方法也可以用来更新/插入记录。
代码:(来自 Meteor)
使用update:
Collection.update({
_id: id
}, {
$set: {
content: 'SomeText'
}
}, {
upsert: true
});
使用upsert:
Collection.upsert({
_id: id
}, {
$set: {
content: 'SomeText'
}
});
问题:
-
区别: 将
upsert标志设置为true的update()和upsert()有什么区别? -
用例: 何时应使用
update()或upsert()
【问题讨论】:
-
嗨@Tushar。如果你有空,请进入chat.stackoverflow.com/rooms/142900/room-for-avi-and-tushar.. :--)