【问题标题】:How to pass json query object from client to server for Meteor collection find如何将 json 查询对象从客户端传递到服务器以进行 Meteor 收集查找
【发布时间】:2016-06-14 06:38:14
【问题描述】:

我有一个依赖于来自客户端的用户输入的复杂查询,然后我形成一个 json 对象来查询集合,并使用更新后的查询再次调用 Meteor.subscribe。

例如在下面的 mongo db 集合中:

{ _id: 1, results: [ { product: "abc", manufacturer: "mfg 1" }, { product: "xyz", manufacturer: "mfg 2" } ] }
{ _id: 2, results: [ { product: "abc", manufacturer: "mfg 3" }, { product: "xyz", manufacturer: "mfg 4" } ] }
{ _id: 3, results: [ { product: "abc", manufacturer: "mfg 5" }, { product: "xyz", manufacturer: "mfg 6" } ] }

用户输入产品:“xyz”,制造商:“mfg 2”,我创建一个查询 JSON 对象,如下所示:

scope.searchTerms = {};
scope.searchTerms.results = {};
var match = {};

match.product = "xyz";
match.manufacturer = "mfg 2";

searchTerms.results.$elemMatch = match;

Meteor.subscribe("products", scope.searchTerms));

那么,我有两个问题:

  1. 服务器总是返回完整的记录集;
  2. searchTerms 始终包含 proto

我复制了searchTerms的内容,然后硬编码供服务器发布,效果很好。

Products = new Mongo.Collection('products');
Meteor.publish('products', function (selector) {
    return Products.find(selector);
});

请帮忙。

【问题讨论】:

  • 您将match 分配给searchTerms 并发送scope.searchTerms。会是这样吗?此外,我将只发送 2 个字符串并在服务器上构建查询(使其更容易验证)。
  • 是的,我也是这么想的,但是,实际的集合有很多这样的子数组,将字符串传递给服务器并从那里构造 json 对象会很头疼,而且它会让客户端构建查询并简单地传递给流星集合发布,然后让 mongodb 处理剩下的事情要容易得多。
  • 好的。对于初学者,请确保服务器正确获取选择器。

标签: angularjs json mongodb meteor


【解决方案1】:

为了回答我自己的问题,我终于找到了问题。

对于要传递给 mongo 的匹配对象,我们不能简单地分配如下值:

var match = {};

match.product = "xyz";
match.manufacturer = "mfg 2";

我们必须采取不同的方式:

var match = {};

match['product'] = "xyz";
match['manufacturer'] = "mfg 2";

真的很奇怪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2015-05-10
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    相关资源
    最近更新 更多