【发布时间】: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));
那么,我有两个问题:
- 服务器总是返回完整的记录集;
- 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