如何在meteor.js中更新Mongo.db集合? [英] how to update a Mongo.db collection in meteor.js?

查看:64
本文介绍了如何在meteor.js中更新Mongo.db集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我需要在用户按下按钮时更新的集合。
我只需要将一个变量更改为另一个变量。

I have a collection that I need to update when the user presses a button. I just need to change one variable to another.

在控制台中,这行代码有效:

In the console, this line of code works:

  db.users.update({username : "Jack"},{age : 13, username : "Jack"});

但当我输入此代码时:

Template.body.events({
'click #updateAge' = function() {
         {
              alert();
              db.users.update({username : "Jack"},{age : 13, username : "Jack"});
         }
    }

})

进入我的Meteor.js的JavaScript文件中,它根本不做任何事情(我没有收到错误消息,我看到警报,但更新不起作用)。

into my JavaScript file for Meteor.js, it doesn't do anything at all (I don't get an error message, and I see the alert, but the update just doesn't work).

我已阅读Meteor文档更新,但我不能似乎让它发挥作用。
有谁知道我在这里做错了什么?

I've read through the Meteor Documentation on updating, but I just can't seem to get it to work. Does anybody know what I'm doing wrong here?

推荐答案

发现问题。

因为我在我的lib.js文件中定义了我的数据库

Since I defined my database in my lib.js file

users = new Meteor.collection(users );

我不需要在 db.users.update前放置一个数据库( {_id:Jack},{...})。我还需要使用给定的mongo _id 找到该文档,而不是标识符username

I don't need to put a db in front of the db.users.update({_id : "Jack"},{...}). I also need to find the document using the given mongo _id, not the identifier "username".

因此适当的代码将是

users.update({_id : "Jack"},{$set:{age : 13, username : "Jack"}});

这篇关于如何在meteor.js中更新Mongo.db集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆