我正在尝试搜索mongo集合以根据变量名称检索一些值,就像这样 sportName =“/ Basketball / i”;
db.Team.find({name:sportName}).forEach(function (team) {
print(team.name);
});
它始终不返回任何内容,但是当我使用以下脚本进行搜索时,它返回结果
db.Team.find({name: /Basketball/i}).forEach(function (team) {
print(team.name);
});
由于
答案 0 :(得分:2)
您无法为这样的变量分配正则表达式:
sportName = "/Basketball/i"; // This is a string.
这样做:
sportName = /Basketball/i; // This is a regex.