Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
using regex to compare vehicle numbers in mongodb

I have a scenario where i want to compare the vehicle number from the database. If the number enterd is KA03ME0101 I will pick up only the number that is 3rd 4th and 7th till last elements from the string and compare it in database where I want to apply regex to do the same thing to number and compare it. Right now i am storing it as String.

   vehicle_number : {
   type : String
    },

And this query does it for me for now

    bookingSchema.statics.findBookingByVehicleNo = function(params, callback){
    return this.findOne({
        '$and' : [
            { 'booking_status' : { '$eq' : 'checked_in' }},
            { 'vehicle_number' : params.vehicle_number   }
        ]
    }).exec(callback);
}

Suppose the vehicle no is `KA03ME0101 , I want to write a query, where I can compare only the numbers that is excluding KA and ME and picking up only the numbers that is 030101. So the first two words are skipped and then pick up the 3rd and 4th number and then skip 5th and 6th and pick up the remaining number. From the user side I will pick up the number in the same way and then compare it. How can i apply regex to do this any lead would be helpful thankyou.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can play around with regular expressions here: http://regexr.com/

For your case, you want to only look at digits, so simply remove non-digits and compare should do the trick:

var plates = [
  'KA03ME0101',
  'AK03EM0101'
];
var regularExpression = /\D/ig;
for (var indexA = 0; indexA < plates.length; indexA++) {
  var a = plates[indexA];
  for (var indexB = indexA + 1; indexB < plates.length; indexB++) {
    var b = plates[indexB];
    if (a.replace(regularExpression, '') == b.replace(regularExpression, '')) {
      console.log(a, 'matches', b);
    }
  }
}

over 4 years ago · Santiago Trujillo Report

0

You can simply extract the numbers from the parameter and query using it. So in your example params.vehicle_number.match(/\d+/g).join('') will give you 030101. So your query should be as follows:

'$and' : [
            { 'booking_status' : { '$eq' : 'checked_in' }},
            { 'vehicle_number' :params.vehicle_number.match(/\d+/g).join('')   }
        ]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!