MongoDB 正則表示式搜尋整數值?
若要對整數值執行正則表示式搜尋,您需要使用 $where 運算子。語法如下
db.yourCollectionName.find({ $where:
"/^yourIntegerPatternValue.*/.test(this.yourFieldName)" });為了理解上述概念,讓我們建立一個帶有文件的集合。建立帶有文件的集合的查詢如下
> db.regExpOnIntegerDemo.insertOne({"StudentId":2341234});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c70370c75eb1743ddddce21")
}
> db.regExpOnIntegerDemo.insertOne({"StudentId":123234});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c70371175eb1743ddddce22")
}
> db.regExpOnIntegerDemo.insertOne({"StudentId":9871234});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c70371875eb1743ddddce23")
}
> db.regExpOnIntegerDemo.insertOne({"StudentId":2345612});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c70372275eb1743ddddce24")
}
> db.regExpOnIntegerDemo.insertOne({"StudentId":1239812345});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c70372975eb1743ddddce25")
}在集合中使用 find() 方法顯示所有文件。查詢如下
> db.regExpOnIntegerDemo.find().pretty();
以下為輸出
{ "_id" : ObjectId("5c70370c75eb1743ddddce21"), "StudentId" : 2341234 }
{ "_id" : ObjectId("5c70371175eb1743ddddce22"), "StudentId" : 123234 }
{ "_id" : ObjectId("5c70371875eb1743ddddce23"), "StudentId" : 9871234 }
{ "_id" : ObjectId("5c70372275eb1743ddddce24"), "StudentId" : 2345612 }
{ "_id" : ObjectId("5c70372975eb1743ddddce25"), "StudentId" : 1239812345 }以下是執行正則表示式搜尋整數值的查詢
> db.regExpOnIntegerDemo.find({ $where: "/^123.*/.test(this.StudentId)" });以下為輸出
{ "_id" : ObjectId("5c70371175eb1743ddddce22"), "StudentId" : 123234 }
{ "_id" : ObjectId("5c70372975eb1743ddddce25"), "StudentId" : 1239812345 }
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP