匹配特定記錄的 MongoDB 正則表示式?
讓我們首先建立一個文件集合 -
> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "John" },"StudentAge":21 });
{
"acknowledged" : true,
"insertedId" : ObjectId("5cf227acb64a577be5a2bc07")
}
> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "JOHN" },"StudentAge":19 });
{
"acknowledged" : true,
"insertedId" : ObjectId("5cf227b8b64a577be5a2bc08")
}
> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "Carol" },"StudentAge":20 });
{
"acknowledged" : true,
"insertedId" : ObjectId("5cf227c2b64a577be5a2bc09")
}以下是使用 find() 方法從集合中顯示所有文件的查詢 -
> dbworkingOfRegularExpressionDemofind();
這將生成以下文件 -
{ "_id" : ObjectId("5cf227acb64a577be5a2bc07"), "StudentDetails" : { "StudentName" : "John" }, "StudentAge" : 21 }
{ "_id" : ObjectId("5cf227b8b64a577be5a2bc08"), "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge" : 19 }
{ "_id" : ObjectId("5cf227c2b64a577be5a2bc09"), "StudentDetails" : { "StudentName" : "Carol" }, "StudentAge" : 20 }
Following is the regular expression to get the document with StudentName JOHN:
> dbworkingOfRegularExpressionDemofind({'StudentDetailsStudentName': /JOHN/});
這將生成以下文件 -
{ "_id" : ObjectId("5cf227b8b64a577be5a2bc08"), "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge" : 19 }
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP