流星 upsert 等价物 [英] Meteor upsert equivalent
问题描述
upsert 命令多久会在 Meteor 中实现?
How soon will the upsert command be implemented in Meteor?
而且,同时做同样事情的最佳方式是什么?
And, what is the best way to do the same thing in the mean time?
我目前正在做的事情是这样的:
Something like this is what I'm doing at the moment:
if typeof ( item = Items.findOne({title:'Foo'}) ) == 'undefined'
item = Items.insert({title:'Foo'})
else
Items.update(item._id, {$set: {title:'Foo'}})
# do something with item
推荐答案
upsert 命令多久会在 Meteor 中实现?
How soon will the upsert command be implemented in Meteor?
更新:@Thomas4019 指出现在支持 upsert
:
UPDATE: @Thomas4019 points out that upsert
is now supported:
v0.6.6
添加 upsert 支持.Collection.update 现在支持 {upsert: true} 选项.此外,添加一个 Collection.upsert 方法,该方法返回新插入的对象 ID(如果适用)."
"Add upsert support. Collection.update now supports the {upsert: true} option. Additionally, add a Collection.upsert method which returns the newly inserted object id if applicable."
来源:History.md
使用文档:http://docs.meteor.com/#upsert
--原始答案如下--
Trello Meteor 路线图上有一张卡片,您可以对其进行投票以表明其对您的重要性:https://trello.com/c/C91INHmj
There is a card on the Trello Meteor Roadmap which you can vote on to indicate its importance to you: https://trello.com/c/C91INHmj
它目前在稍后"列表中,这意味着它需要一段时间才能实施,除非它收到很多选票.
It is currently in the "Later" list which means it will be a while before it is implemented unless it receives a lot of votes.
另一个需要注意的重要事项是,由于meteor 是开源的,您可以自己实施必要的更改并提交回来.
The other important thing to note is that since meteor is open-source, you could implement the necessary changes yourself and submit back.
同时做同样事情的最佳方式是什么?
What is the best way to do the same thing in the mean time?
有多种解决方案,但如果不了解您的设计,就无法确定哪种最适合您的用例.
There are several solutions but which is most appropriate for your use-case is impossible to tell without more knowledge of your design.
按原样使用代码,为集合添加唯一索引,并在出现重复键错误时处理
Use the code as is, add an unique index to the collection, and handle the duplicate key error if/when it arises
更改设计以实现显式乐观并发.
Change design to implement explicit optimistic concurrency.
这两种解决方案的核心是一样的,优雅地处理错误情况.#1 更容易实现.#2 允许在如何处理乐观并发方面具有更大的灵活性.
The core of both of these solutions is the same, gracefully handle the error case. #1 is easier to implement. #2 allows for greater flexibility in how the optimistic concurrency is handled.
这篇关于流星 upsert 等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!