Meteor mongo得到这个值

时间:2016-06-22 11:05:47

标签: mongodb meteor

我有一个流星游戏 enter image description here

在服务器上我有一个调用meteor方法moveFish的计时器。

Meteor.startup(() => {
    Meteor.setInterval(function(){
        Meteor.call("moveFish")
    }, 40);
});

该方法选择所有鱼类活着并让它们移动

Meteor.methods({
    moveFish: function(id, speed) {
        Meteor.users.update( { "fish.alive": true }, { $inc: { "fish.positionX": 2 } } )
    }
})

enter image description here

如何使用 this.fish.speed 代替值2

来移动鱼类
Meteor.users.update( { "fish.alive": true }, { $inc: { "fish.positionX": 2 } } )

*注意不起作用

Meteor.users.update( { "fish.alive": true }, { $inc: { "fish.positionX": "fish.speed" } } )

这是有效的

Meteor.users.find().map( function(user) { x = user.fish.speed Meteor.users.update(user, {$inc: {"fish.positionX": x} }) })

1 个答案:

答案 0 :(得分:0)

遗憾的是,文档无法在更新操作中使用对自身的引用。

在这种情况下,您需要先找到它并手动遍历所有文档:

Meteor.users.findAll({ "fish.alive": true }).fetch().forEach(function(fish) {
  fish.positionX += fish.speed;
  fish.save();
});