如何在MongoDB上进行不区分大小写的查询?
使用regexp在MongoDB上进行不区分大小写的查询。为了理解这个概念,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.caseInsensitiveDemo.insertOne({"UserName":"David"});{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f6fec8d10a061296a3c45")
}
> db.caseInsensitiveDemo.insertOne({"UserName":"DAVID"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f6ff28d10a061296a3c46")
}
> db.caseInsensitiveDemo.insertOne({"UserName":"david"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f6ffa8d10a061296a3c47")
}
> db.caseInsensitiveDemo.insertOne({"UserName":"Carol"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f70008d10a061296a3c48")
}
> db.caseInsensitiveDemo.insertOne({"UserName":"Mike"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f70058d10a061296a3c49")
}
> db.caseInsensitiveDemo.insertOne({"UserName":"Sam"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f70098d10a061296a3c4a")
}
> db.caseInsensitiveDemo.insertOne({"UserName":"daVID"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f70138d10a061296a3c4b")
}
在find()
method的帮助下显示集合中的所有文档。查询如下-
> db.caseInsensitiveDemo.find().pretty();
以下是输出-
{ "_id" : ObjectId("5c6d7a67f2db199c1278e7ef"), "Name" : "John" }{ "_id" : ObjectId("5c6d7ad6f2db199c1278e7f0"), "Name" : "JOHN" }
{ "_id" : ObjectId("5c7f6fec8d10a061296a3c45"), "UserName" : "David" }
{ "_id" : ObjectId("5c7f6ff28d10a061296a3c46"), "UserName" : "DAVID" }
{ "_id" : ObjectId("5c7f6ffa8d10a061296a3c47"), "UserName" : "david" }
{ "_id" : ObjectId("5c7f70008d10a061296a3c48"), "UserName" : "Carol" }
{ "_id" : ObjectId("5c7f70058d10a061296a3c49"), "UserName" : "Mike" }
{ "_id" : ObjectId("5c7f70098d10a061296a3c4a"), "UserName" : "Sam" }
{ "_id" : ObjectId("5c7f70138d10a061296a3c4b"), "UserName" : "daVID" }
这是在MongoDB上进行不区分大小写查询的查询-
> db.caseInsensitiveDemo.find({UserName: /^david$/i } ).pretty();
以下是输出-
{ "_id" : ObjectId("5c7f6fec8d10a061296a3c45"), "UserName" : "David" }{ "_id" : ObjectId("5c7f6ff28d10a061296a3c46"), "UserName" : "DAVID" }
{ "_id" : ObjectId("5c7f6ffa8d10a061296a3c47"), "UserName" : "david" }
{ "_id" : ObjectId("5c7f70138d10a061296a3c4b"), "UserName" : "daVID" }
以上是 如何在MongoDB上进行不区分大小写的查询? 的全部内容, 来源链接: utcz.com/z/343365.html