我正在关注Angular Meteor教程,它具有以下代码:
import { Meteor } from 'meteor/meteor';
import { Counts } from 'meteor/tmeasday:publish-counts';
import { Parties } from './collection';
if (Meteor.isServer) {
Meteor.publish('parties', function(options, searchString) {
const selector = {
$or: [{
// the public parties
$and: [{
public: true
}, {
public: {
$exists: true
}
}]
}, {
// when logged in user is the owner
$and: [{
owner: this.userId
}, {
owner: {
$exists: true
}
}]
}]
};
if (typeof searchString === 'string' && searchString.length) {
selector.name = {
$regex: `.*${searchString}.*`,
$options : 'i'
};
}
Counts.publish(this, 'numberOfParties', Parties.find(selector), {
noReady: true
});
return Parties.find(selector, options);
});
}
我正在尝试修改selector.name以包括描述字段。我已经检查了添加选择器。描述,但是什么也没做。在将正则表达式与文档的任一字段匹配时,是否可以修改选择器以包括搜索多个字段?
“两个”都可能意味着$or:
-如果您仅添加description
到对象,则最终会得到一个$and:
在获得名称和描述标准之前,您的选择器将是:
let selector =
{ $or:
[
{ $and: [ { public: true }, { public: { $exists: true } } ] },
{ $and: [ { owner: this.userId }, { owner: { $exists: true } }] }
]
};
(请注意,let
而不是const
)
为此,您想$and:
附加一秒钟$or:
if (typeof searchString === 'string' && searchString.length) {
selector = { $and:
[
selector,
{ $or:
[
{ name: { $regex: `.*${searchString}.*`, $options : 'i' }},
{ description: { $regex: `.*${searchString}.*`, $options : 'i' }},
]
}
]
};
}
最后,您应该具有一个类似于以下内容的对象:
{ $and:
[
{ $or: [ public criteria, owner criteria ] },
{ $or: [ name regex, description regex ] }
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句