流星:将Mongo Selector从客户端传递到服务器的最佳方法 [英] Meteor: Best Method for Passing Mongo Selector from Client to Server

查看:80
本文介绍了流星:将Mongo Selector从客户端传递到服务器的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面的mongo集合(Foo(X)==键; Bars ==值):编辑-我来自关系数据库背景.显然,我的收藏看起来不像下面这样,但是您知道了...

I have a mongo collection like the following (Foo(X) == keys; Bars == values): EDIT- I come from a relational database background. Obviously my collection doesn't look like the below, but you get the idea...

+--------+--------+--------+
|  Foo1  |  Foo2  |  Foo3  |
+--------+--------+--------+
| Barbar | Barbar |   Bar  |
|   bar  |  Bar   | BarBar |
|   Bar  | barbar | barBar |
|   ...  |   ...  |   ...  |

对我来说,允许客户过滤数据非常重要.有时,所有列都会有一个过滤器,有时,没有列会有一个过滤器,介于两者之间.目前,我正在按以下方式处理该问题:

It's important for me to allow my client to filter the data. Sometimes, all columns will have a filter, other times no columns will have a filter and anywhere in between. Currently, I'm handling the issue as follows:

客户

Var aFoo1Filter = ["bar"]
Var aFoo2Filter = ["Barbar", "BarBar"]
Var aFoo3Filter = ["barbar", "bar"]
//Where the user can affect the array through some other code

服务器

Meteor.publish("foos", function (aFoo1Filter, aFoo2Filter, aFoo3Filter ) {
  return FooCl.find({Foo1: {$in: aFoo1Filter},
                     Foo2: {$in: aFoo2Filter},
                     Foo3: {$in: aFoo3Filter}}, 
                    {limit: 10});
});

我希望通过仅将一个对象或一个字符串从客户端传递到服务器来简化此操作,但是都没有成功.在下面查看我的尝试:

I'm hoping to simplify this by passing through just one object or one string from the client to the server, but neither attempts have worked. See my attempts, below:

尝试#1-传递字符串

客户

Var sFilter = "Foo1: {$in: [\"bar\"]},
               Foo2: {$in: [\"Barbar\", \"BarBar\"]},
               Foo3: {$in: [\"barbar\", \"bar\"]}"

服务器

Meteor.publish("foos", function (sFilter) {
  return FooCl.find({sFilter}, 
                    {limit: 10});
});

/////////////////

//////////////////

尝试#2-通过对象

客户

var oFilter = {
  Foo1: "bar"
}

服务器

Meteor.publish("foos", function (oFilter) {
  return FooCl.find({oFilter}, 
                    {limit: 10});
});

我目前没有随身携带的机器,所以我无法提供有关抛出的错误类型的更多详细信息.希望今晚有更多信息.感谢您的帮助!

I don't have my machine with me at the moment, so I can't provide more detail on the type of error thrown. Hopefully will have some more info up tonight. Thanks for any help!

推荐答案

解决此问题的最简单方法是使用选择器进行订阅:

The easiest way to solve this problem is to subscribe using a selector:

var selector = {Foo1: {$in: aFoo1Filter}, Foo2: {$in: aFoo2Filter}};
Meteor.subscribe('foos', selector);

服务器

Meteor.publish('foos', function (selector) {
  return FooCl.find(selector, {limit: 10});
});

但是,重要的是要认识到,这使客户能够从她想要的FooCl集合中请求任何个文档.一种改进的解决方案是通过在选择器上使用 match 来限制请求内容.例如:

However, it's important to recognize that this gives the client the ability to request any documents from the FooCl collection that she wants. An improved solution is to limit what can be requested by using match on the selector. For example:

Meteor.publish('foos', function(selector) {
  check(selector, {
    Foo1: Match.Optional({$in: [String]}),
    Foo2: Match.Optional({$in: [String]})
  });

  if (!_.isEmpty(selector)) {
    return FooCl.find(selector, {limit: 10});
  }
});

这将确保在将任何文档发送到客户端之前,selector符合可接受的格式.

This will ensure that selector conforms to an acceptable pattern before any documents are sent to the client.

这篇关于流星:将Mongo Selector从客户端传递到服务器的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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