我已经能够在Meteor中实现一种发布方法,该方法在template.js中订阅时通过给定属性运行对我的mongo集合的查询,这很好但现在我想添加以相同方式搜索多个属性。所以,让我们说我在Mongo有一个集合,其中的文档都具有相同的属性,但具有不同的值。
{batch:'HAHT020614' color: 'blue', material: 'plastic', printing: true,
model: 'H100', handle: 'plastic', product: 'C010' }
{batch:'HBTH060614' color: 'red', material: 'metal', printing: false,
model: 'V400', handle: 'metal', product: 'P001' }
...
我试图将对象发送到publish方法,该方法包含所有用户通过反应变量选择的字段:
Template.inventory.onCreated( function appBodyOnCreated() {
this.searchQuery = new ReactiveVar({
color: anyItem,
batch: anyItem,
model: anyItem,
material: anyItem,
handle: anyItem,
printing: anyItem,
product: anyItem,
});
this.autorun(function () {
let template = Template.instance();
template.subscribe("stock.search", template.searchQuery.get());
});
});
然后在publication.js:
Meteor.publish('stock.search', function stockQuery(search) {
return Stock.find(
{ $and: [
{color: { $regex : search.color }},
{batch: { $regex : search.batch}},
{product: { $regex : search.product}},
{model: { $regex : search.model}},
{material: { $regex : search.material}},
{handle: { $regex : search.handle}},
{printing: { $regex : search.printing}}
]
},
{ limit: 10, sort: { batch: 1 } });
});
问题在于,根据用户的需要,将在应用程序中使用或不使用某些搜索字段,从而可以搜索所有项目,例如蓝色和制作或金属,只需混合搭配任何必要的东西即可。
对象正确到达发布方法,我能够提取属性,但问题在于查询,因为我不知道是否可以要求Mongo匹配某些属性为"任何"。我试图传递{$ exists:true}作为默认值(当搜索字段为空时)属性,以便它与集合中的任何文档匹配,但查询似乎没有正确返回。在这种情况下,我使用正则表达式作为某种"包含"而var anyItem只是一个空字符串。
是否有正确的方法来查询mongo以仅将某些属性与所选值匹配,而其他属性保持为"任何"?
答案 0 :(得分:1)
您只能将非空条件传递给publish方法,并使用如下给定条件构建查询:
Meteor.publish('stock.search', function stockQuery(search) {
const criteria = Object.keys(search).map(k => ({ [k]: { $regex: search[k] } }));
return Stock.find(
{ $and: criteria },
{ limit: 10, sort: { batch: 1 } }
);
});