use*_*495 78 regex database mongodb
var thename = 'Andrew';
db.collection.find({'name':thename});
Run Code Online (Sandbox Code Playgroud)
如何查询不区分大小写?即使"安德鲁",我想找到结果;
dcr*_*sta 104
Chris Fulstow的解决方案将起作用(+1),但是,它可能效率不高,特别是如果你的收藏非常大.非root的正则表达式(那些不是以正则表达式开头^,将正则表达式锚定到字符串的开头),以及那些使用i不区分大小写的标志的表达式将不使用索引,即使它们存在.
您可能考虑的另一种选择是对数据进行非规范化以存储name字段的小写版本,例如name_lower.然后,您可以有效地查询(特别是如果它被索引)对不区分大小写的完全匹配,例如:
db.collection.find({"name_lower": thename.toLowerCase()})
Run Code Online (Sandbox Code Playgroud)
或者使用前缀匹配(带根的正则表达式):
db.collection.find( {"name_lower":
{ $regex: new RegExp("^" + thename.toLowerCase(), "i") } }
);
Run Code Online (Sandbox Code Playgroud)
这两个查询都将使用索引name_lower.
Chr*_*tow 74
你需要对这个使用不区分大小写的正则表达式,例如
db.collection.find( { "name" : { $regex : /Andrew/i } } );
Run Code Online (Sandbox Code Playgroud)
要使用thename变量中的正则表达式模式,请构造一个新的RegExp对象:
var thename = "Andrew";
db.collection.find( { "name" : { $regex : new RegExp(thename, "i") } } );
Run Code Online (Sandbox Code Playgroud)
更新:要完全匹配,您应该使用正则表达式"name": /^Andrew$/i.感谢Yannick L.
RIP*_*PAN 28
我已经解决了这个问题.
var thename = 'Andrew';
db.collection.find({'name': {'$regex': thename,$options:'i'}});
Run Code Online (Sandbox Code Playgroud)
如果你想查询'不区分大小写的完全匹配'那么你可以这样做.
var thename = '^Andrew$';
db.collection.find({'name': {'$regex': thename,$options:'i'}});
Run Code Online (Sandbox Code Playgroud)
MongoDB 3.4现在包含创建真实的不区分大小写索引的功能,这将大大提高大型数据集上不区分大小写的查询的速度。通过指定强度为2的排序规则来完成。
可能最简单的方法是在数据库上设置排序规则。然后所有查询都继承该排序规则并将使用它:
db.createCollection("cities", { collation: { locale: 'en_US', strength: 2 } } )
db.names.createIndex( { city: 1 } ) // inherits the default collation
Run Code Online (Sandbox Code Playgroud)
您也可以这样:
db.myCollection.createIndex({city: 1}, {collation: {locale: "en", strength: 2}});
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
db.myCollection.find({city: "new york"}).collation({locale: "en", strength: 2});
Run Code Online (Sandbox Code Playgroud)
这将返回名为“纽约”,“纽约”,“纽约”等的城市。
有关更多信息:https : //jira.mongodb.org/browse/SERVER-90
要查找不区分大小写的字符串,请使用它,
var thename = "Andrew";
db.collection.find({"name":/^thename$/i})
Run Code Online (Sandbox Code Playgroud)
与猫鼬(和节点),这工作:
User.find({ email: /^name@company.com$/i })
User.find({ email: new RegExp(`^ $ {emailVariable} $`,'i')})
在MongoDB中,这有效:
db.users.find({ email: { $regex: /^name@company.com$/i }})这两行都不区分大小写。数据库中的电子邮件可能是NaMe@CompanY.Com,这两行仍将在数据库中找到对象。
同样,我们可以使用/^NaMe@CompanY.Com$/i并且它仍然可以name@company.com在数据库中找到电子邮件:。
小智 5
几个小时前我刚刚解决了这个问题.
var thename = 'Andrew'
db.collection.find({ $text: { $search: thename } });
Run Code Online (Sandbox Code Playgroud)
您甚至可以通过以下方式选择Andrew的用户对象所需的字段来扩展此功能:
db.collection.find({ $text: { $search: thename } }).select('age height weight');
Run Code Online (Sandbox Code Playgroud)
参考:https://docs.mongodb.org/manual/reference/operator/query/text/#text
...在 NodeJS 上使用猫鼬查询:
const countryName = req.params.country;
{ 'country': new RegExp(`^${countryName}$`, 'i') };
Run Code Online (Sandbox Code Playgroud)
或者
const countryName = req.params.country;
{ 'country': { $regex: new RegExp(`^${countryName}$`), $options: 'i' } };
// ^australia$
Run Code Online (Sandbox Code Playgroud)
或者
const countryName = req.params.country;
{ 'country': { $regex: new RegExp(`^${countryName}$`, 'i') } };
// ^turkey$
Run Code Online (Sandbox Code Playgroud)
Javascript 中的完整代码示例,NodeJS 和 Mongoose ORM on MongoDB
// get all customers that given country name
app.get('/customers/country/:countryName', (req, res) => {
//res.send(`Got a GET request at /customer/country/${req.params.countryName}`);
const countryName = req.params.countryName;
// using Regular Expression (case intensitive and equal): ^australia$
// const query = { 'country': new RegExp(`^${countryName}$`, 'i') };
// const query = { 'country': { $regex: new RegExp(`^${countryName}$`, 'i') } };
const query = { 'country': { $regex: new RegExp(`^${countryName}$`), $options: 'i' } };
Customer.find(query).sort({ name: 'asc' })
.then(customers => {
res.json(customers);
})
.catch(error => {
// error..
res.send(error.message);
});
});
Run Code Online (Sandbox Code Playgroud)