如何仅查看猫鼬中值的首字母来查找所有文档

how to find all documents with only looking at first letter of the values in mongoose

我想通过首字母查找所有文档。

情况是当我在 mysql 中查询时,我可以 WHERE Stores LIKE 'a%'

the result would be all data with the first letter a.

问题是:

有一个用于该目的的 $regex 运算符,查询将如下所示:

Model.find({"name": {$regex: /^a/, $options: 'i'}}).exec(callback);

可以简化为:

Model.find({"name": /^a/i}}).exec(callback);

如果您需要在查询中传递变量:

var char = 'a';
Model.find({"name": {$regex: '^' + char, $options: 'i'}}).exec(callback);