这个函数可以再短一点吗?我不喜欢 if/else 更新,这几乎是相同的。唯一的区别是 'status.edited': false
仅应在 method = reset 时设置。否则不应修改。
function updateVersion(id, method) {
var timestamp = new Date().getTime();
if (method == 'reset') {
Collection.update(
{ _id: id },
{
$set: { 'status.version': timestamp, 'status.edited': false },
$unset: { 'status.editing': '' }
}
);
}
else {
Collection.update(
{ _id: id },
{
$set: { 'status.version': timestamp },
$unset: { 'status.editing': '' }
}
);
}
}
最佳答案
您可以像这样动态构建查询:
function updateVersion(id, method) {
var timestamp = new Date().getTime();
var update = {
'$set': { 'status.version': timestamp },
'$unset': { 'status.editing': '' }
};
if (method === 'reset')
update['$set']['status.edited'] = false;
Collection.update( { '_id': id }, update );
}
关于javascript - 使用 if/else mongodb-update 使 JS 函数更短,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959678/