我有一个猫鼬查询如下
Computer
.find()
.where('OS').equals('Windows')
.exec(function(err, items) {
});
它返回操作系统的所有computer
记录Windows
。
现在,我想使用一个变量 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
将两种查询类型合并为一种,但是我希望这会对性能产生影响。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句