$ where mongo 操作员在meteorjs 中工作 [英] $where mongo operator work around in meteorjs

查看:60
本文介绍了$ where mongo 操作员在meteorjs 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a MongoDB query that works perfectly in MongoDB shell

 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 中实现这一点,有什么解决办法?

How can I implement this in MeteorJS what is the work around?

根据要求的解释:我需要在服务器端有一个函数,它是否在客户端工作无关紧要.该函数必须将给定的数组与文档上的 numbers 字段匹配,并告诉我数组中有多少个数字匹配以及文档 ID 或类似内容:

Explanations as per request: I need a function in the serverside, does not matter if it works in the client side. The function has to match the given array with the numbers field on the document and tell me how many numbers in the array are matching and the document ID or something like this :

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

试过这个:在(流星是服务器)中添加了代码:

Tried this: Added the code in (meteor is server):

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 中完美运行.

When I do .fetch() all the documents will print out which should not happen this code works perfectly in mondodb shell.

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

Solution thank's to @AndrewMao: 1st solution: By adding the .toString() in the end of the function the .toString() as I tested works perfectly in 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();

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

2nd solution: This is if you want to add you custom array or something else in the query:

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

You have to pass your function as a string in one line, also you can add other string in it as I did with the random array. Look at the function call (isInList(num,['+ random +']);}') in the example below.

例如:

 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();

推荐答案

您的问题可能与 $where 查询无关;这是因为您打印的是游标而不是匹配的文档.即,将 .fetch() 附加到 .find() 的末尾:http://docs.meteor.com/#fetch

Your problem is probably not with the $where query; it's because you are printing out a cursor rather than the matched documents. Namely, append .fetch() to the end of the .find(): http://docs.meteor.com/#fetch

游标是对选定文档组的引用,在您尝试 .map().fetch().fetch().forEach() 就可以了.

A cursor is a reference to a selected group of documents and does not actually grab the documents until you try to .map(), .fetch(), or .forEach() on it.

此外,您需要将 $where 函数作为字符串传递,而不是作为实际函数:https://groups.google.com/forum/#!topic/meteor-talk/ErgCC4g_bB0.这是 Mongo 处理和运行函数所必需的(在 Node 中没有完成.)

Additionally, you need to pass the $where function as a string, not as an actual function: https://groups.google.com/forum/#!topic/meteor-talk/ErgCC4g_bB0. This is needed for Mongo to process and run the function (it is not being done in Node.)

Tickets.find({ $where: 'function() { ... }' });

您将无法获得源代码突出显示的好处,但您测试的函数应该可以工作.你也可以试试

You won't be able to have the benefit of source code highlighting, but the function you tested should work. You can also try

Tickets.find({ $where: function() { ... }.toString() });

但这可能只适用于浏览器,不适用于 Node.

but this may only work in browsers and not in Node.

这篇关于$ where mongo 操作员在meteorjs 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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