迭代多个流星集合

Iterate over multiple Meteor Collections

本文关键字:集合 流星 迭代      更新时间:2023-09-26

我想在meteor(服务器端)中迭代多个Mongo集合。首先我想查一查某个collection是否有单据。

我的代码:

     var isEmptyCollection = function(name) {
          if(name.find().count() === 0) {
             return true
          } else {
             return false
          }
        };
        var mycollections = ["CollectionOne", "CollectionTwo", "CollectionThree"];

        for (var i = 0; i < mycollections.length; i++) {
            if (isEmptyCollection(mycollections[i])) {
        } else {
            var data = mycollections[i].find({},{fieldOne: 1}).fetch();
            console.log(data);
        }

我得到这个错误:

    TypeError: Object CollectionOne has no method 'find'....

如果一个集合有任何值,我如何在循环中遍历集合/检查?

集合数组包含许多字符串,但它应该包含一个集合对象列表。尝试将数组赋值更改为:

var mycollections = [CollectionOne, CollectionTwo, CollectionThree];

我假设您已经使用Mongo.Collection定义了这些

mycollections[i]将是字符串"CollectionOne"。使用global[ mycollections[i] ]获取对实际集合的引用。

E。g: global[ mycollections[i] ].find().count()

客户端是window[ mycollections[i] ]