इंटीजर वैल्यू पर रेगेक्स खोज करने के लिए, आपको $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 }