MongoDB:查詢與正則表示式輸入類似的名稱?
在 MongoDB 中使用 $regex 傳送名稱以查詢與輸入類似的名稱。讓我們建立一個包含文件的集合 -
> db.demo514.insertOne({"Information":{"FullName":"John Doe"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e885116987b6e0e9d18f58c")
}
> db.demo514.insertOne({"Information":{"FullName":"John Smith"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e88515e987b6e0e9d18f58d")
}
> db.demo514.insertOne({"Information":{"FullName":"john doe"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e885169987b6e0e9d18f58e")
}
> db.demo514.insertOne({"Information":{"FullName":"Chris Brown"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e88516f987b6e0e9d18f58f")
}使用 find() 方法從集合中顯示所有文件 -
> db.demo514.find();
這將產生以下輸出 -
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } }
{ "_id" : ObjectId("5e88515e987b6e0e9d18f58d"), "Information" : { "FullName" : "John Smith" } }
{ "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } }
{ "_id" : ObjectId("5e88516f987b6e0e9d18f58f"), "Information" : { "FullName" : "Chris Brown" } }以下是查詢與輸入類似名稱的查詢 -
> db.demo514.find({'Information.FullName': {$regex: "John Doe", $options: 'i'}});這將產生以下輸出 -
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } }
{ "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } }
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP