我有一个猫鼬查询如下
Computer
.find()
.where('OS').equals('Windows')
.exec(function(err, items) {
});
它使用Windows OS返回所有计算机记录.
现在,我想使用变量osType替换equals参数,以使其更加灵活.
我可以为osType变量提供通配符*吗?我测试了它,但是它不起作用.
var osType = '*';
Computer
.find()
.where('OS').equals(osType)
.exec(function(err, items) {
});
或有什么替代方法可以做到这一点?
请不要删除where子句,因为我希望它用于osType = windows,linux …等…
解决方法:
我认为您将不得不在以下两个语句之间切换:
// If you have a specific value you want to find:
Computer.find().where('OS').equals(osType).exec(...)
和:
// All documents that have a `OS` property:
Computer.find().where('OS').exists().exec(...)
您可以重写代码以适应以下情况:
var query = Computer.find().where('OS');
if (osType === '*') {
query = query.exists();
} else {
query = query.equals(osType);
}
query.exec(...);
另外,您可以使用Query#regex
将两种查询类型合并为一种,但是我希望这会对性能产生影响.