Mongo DB 增量 if 语句(Meteor App)
Mongo DB increment if statement (Meteor App)
我似乎无法绕过这个看似简单的问题。
下面的代码允许我在帖子对象上将喜欢的数量加减 1。
我希望能够做到以下几点:
如果 postLikes = 0,则停止允许我减少喜欢的数量。
我不希望能够将喜欢的数量减少到 0 以上。
这是我的代码:
Template.post.events({
"click .likeButton": function() {
// Set the checked property to the opposite of its current value
Posts.update(this._id, {
$inc: {
postLikes: 1
}
});
},
"click .dislikeButton": function() {
// Set the checked property to the opposite of its current value
Posts.update(this._id, {
$inc: {
postLikes: -1
}
});
}
});
那么,在更新之前有一个条件呢?
更新 - 没关系。实现此目的的更好方法是通过查询!
"click .dislikeButton": function() {
Posts.update({_id : this._id, postLikes : {$gt : 0}}, {
$inc: {
postLikes: -1
}
});
}
仅当文档与选择器"update"的第一个参数中的条件匹配时,才会应用修饰符。 我看到了接受的答案,但实际上只使用一个数据库调用,并在可能的情况下让数据库检查对象条件。 喜欢这个:
"click .dislikeButton": function() {
Posts.update({ _id: this._id, postLikes:{ $gt: 0 }}, { $inc: { postLikes: -1 }});
}
请参阅有关Mongo更新和Meteor更新的文档。
相关文章:
- Meteor如何接收HTTP请求
- Meteor-将选定窗体中的对象添加到集合中
- Meteor上的启动页面
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- Meteor-添加用户自定义字段的方法不起作用
- Meteor忘记了密码的实现
- 链接所有<a>Meteor
- 在Meteor项目中安装并包含npm模块后出错
- Meteor JS中代码的重复使用部分
- Meteor Router数据函数被调用两次
- 基于localStorage的Meteor激活模板
- Meteor:全局常量未从app/lib/_constants.js中获取
- Meteor.js Facebook使用“Apple-mobile-web-app-able”登录
- Mongo DB 增量 if 语句(Meteor App)
- 调用()从我的app.meteor.com
- Meteor-App: Tracker is not defined
- 为Meteor App添加css框架
- 在Meteor App中控制器应该放置的位置
- Meteor Basic App: ReferenceError: Entries is not defined
- 如何模拟“文档”对象在Meteor App中进行测试