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

查看:22
本文介绍了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: 假}).事实证明,这比我想象的要麻烦得多.我想做这样的事情(使用虚拟变量名称):

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天全站免登陆