$ mongo运营商在meteorjs中工作

时间:2014-06-23 11:34:59

标签: mongodb meteor

我有一个在MongoDB shell中完美运行的MongoDB查询

 db.collection.find({ $where: 
function(){
    var num = this.numbers;
    function isInList(numbers,matchArray) 
        {
            var reducedNums = numbers.filter(function(num) {
            return matchArray.indexOf(num) !== -1
            });
            if (reducedNums.length == 7){
                return true;
            } else {
                return false;
            }
        }
    return isInList(num, [ 28 ,5, 17, 47, 1, 24, 37, 19, 4, 3 ] );
    }
}
);

我如何在MeteorJS中实现这个功能?

根据要求说明: 我需要在服务器端使用一个函数,如果它在客户端工作则无关紧要。 该函数必须将给定数组与文档上的数字字段相匹配,并告诉我数组中有多少数字匹配,文档ID或类似内容:

if (match.length == 7){
                return "DOCUMENT";
            } else {
                return false;
            }

试过这个: 添加了代码(meteor是服务器):

console.log(Tickets.find({ $where: 
function(){
        var num = this.numbers;
        function isInList(numbers,matchArray) 
            {
                var reducedNums = numbers.filter(function(num) {
                return matchArray.indexOf(num) !== -1
                });
                if (reducedNums.length == 7){
                    return true;
                } else {
                    return false;
                }
            }
        return isInList(num, [ 28 ,5, 17, 47, 1, 24, 37, 19, 4, 3 ] );
        }
    }
))

输出是这样的:

 I20140623-17:15:02.690(2)? { _mongo:
 I20140623-17:15:02.910(2)?    { _connectCallbacks: [ [Function] ],
 I20140623-17:15:02.911(2)?      _observeMultiplexers: {},
 I20140623-17:15:02.912(2)?      _onFailoverHook: { nextCallbackId: 0, callbacks: {} },
 I20140623-17:15:02.913(2)?      _docFetcher: { _mongoConnection: [Circular], _callbacksForCacheKey: {} },
 I20140623-17:15:02.914(2)?      _oplogHandle:
 ...
 ...
 ...
 I20140623-17:15:03.025(2)?      selector: { '$where': [Function] },
 I20140623-17:15:03.027(2)?      options: { transform: null } },
 I20140623-17:15:03.030(2)?   _synchronousCursor: null }
 => Meteor server restarted

当我执行.fetch()时,所有文档都会打印出来,不应该发生这个代码在mondodb shell中完美运行。

解决方案感谢@AndrewMao: 第一解决方案 通过在函数末尾添加.toString(),我测试的.toString()在NodeJS中完美运行

例如:

Tickets.find({'$where':     function(){
    var num = this.numbers;
    function isInList(numbers,matchArray) 
        {
            var reducedNums = numbers.filter(function(num) {
            return matchArray.indexOf(num) !== -1
            });
            if (reducedNums.length == 7){
                return true;
            } else {
                return false;
            }
        }
    return isInList(num, [ 28 ,5, 17, 47, 1, 24, 37, 19, 4, 3 ] );
    }.toString();
}).fetch();

第二个解决方案:如果您想在查询中添加自定义数组或其他内容:

你必须将你的函数作为一个字符串传递给一行,你也可以在其中添加其他字符串,就像我对随机数组一样。在下面的例子中查看函数调用(isInList(num,['+ random +']);}')。

例如:

 var random = [ 19 ,3, 14, 24, 2, 38, 48, 19, 4, 3 ];

 Tickets.find({'$where': 'function(){var num = this.numbers; function isInList(numbers,matchArray) {var reducedNums = numbers.filter(function(num) { return matchArray.indexOf(num) !== -1  }); if (reducedNums.length == 7){ return true;} else {  return false;   }}return isInList(num,['+ random +']);}'}).fetch();

0 个答案:

没有答案
相关问题