Meteor / Mongo:查找和更新集合中的某些元素 [英] Meteor/Mongo: Finding and updating certain elements in a collection

查看:108
本文介绍了Meteor / Mongo:查找和更新集合中的某些元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Meteor并需要Mongo的帮助。我有一组名称,我正在列表中显示,并希望能够根据其他条件更新数据库中某些条目的一个变量。基本上我想要做的是:

I'm starting off with Meteor and need some help with Mongo. I have a collection of names that I'm displaying on a list and want to be able to update one variable of certain entries in the database based on other criteria. Basically what I want to do is:

对于特征A = true且B = true的每个条目,将特征C改为false。

For every entry where characteristic A = true and B = true, change characteristic C to be false.

到目前为止,我一直试图弄清楚Mongo如何处理集合元素的for each循环,并且每个元素检查条件A和B是否成立,然后collection.update(element,{C:false})。事实证明这比我想象的要多得多。我想做这样的事情(使用虚拟变量名):

So far, I've been trying to figure out how Mongo can handle a "for each" loop over the elements of the collection, and for each element check if conditions A and B hold, and then collection.update(element, {C: false}). This is proving to be a lot more problematic than I thought. I want to do something like this (using dummy variable names):

for (i = 0; i < collection.find().count(); i++){
    if (collection[i].A===true && collection[i].B===true)
        collection.update(collection[i], {$set: {C: false}});
};

我一直在改变这个基本代码,但我开始意识到我错过了什么关于索引/集合如何在Mongo中工作的基础知识。你可以为这样的集合编制索引(如果是这样,这甚至是做我正在尝试做的最方便的方法吗?)?

I've been changing this base code around, but am starting to sense that I'm missing something basic about indexing/how collections work in Mongo. Can you index a collection like this (and if so, is this even the most convenient way to do what I'm trying to do?)?

推荐答案

当然我想知道如何在发布之后立即执行此操作,当然这是在Meteor文档中建议的!

Of course I figure out how to do this right after posting, and of course it's suggested in the Meteor documentation!

当然,它是一个简单的解决方案:

And, of course, it's a simple solution:

collection.update({A: true, B: true}, {$set: {C:false}});

这篇关于Meteor / Mongo:查找和更新集合中的某些元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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