我有这个代码:
Playlist.aggregate([
$match: {
$or: [{genre: new RegExp(["Pop", "Indie"], 'i')}]
}
]).exec(function(err, playlists){
console.log(playlists);
});
问题是它返回一个空数组,但是当我删除 RegExp
(Pop/Indie) 中的条件之一时,它会正确返回。
我在正则表达式中做错了什么?我该如何解决这个问题?
最佳答案
尝试在单一模式中使用交替:
Playlist.aggregate([
$match: {
$or: [{genre: new RegExp("Pop|Indie", 'i')}]
}
]).exec(function(err, playlists){
console.log(playlists);
});
但是,如果您希望模式保持不变,则可能需要使用正则表达式,而不是使用 RegExp
。
关于javascript - 将 RegExp 与多个条件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51757758/