I want mongodb with regex

0

I'm trying to find the best way to make this search in mongodb. I have this number 42999234180 and I have a table of prefixes that range from 3 to 7 characters, I would like that when searching for 4299234180 it would return the value that was more similar to the integer, for example in the database I have saved

429
4299
42999 <--- nesse caso ele retornaria esse resultado.
429998
    
asked by anonymous 21.03.2017 / 21:49

2 answers

2

I think you're looking for something like string search for approximate result (try searching for "approximate string matching"). Thinking for a moment, if you look for the whole number there is no possibility of finding "minor" results, unless you are using some approximate result.

There are some techniques for this, I used this algorithm in a project. It compares pairs of letters within your String. Take a look.

    
05.04.2017 / 21:48
0

What you want does not have to do with REGEX since it is necessary to perform arithmetic operations.

Resolution

CREATE TABLE prefixos(
  prefixo INTEGER
);

INSERT INTO prefixos (prefixo) VALUES
('429'),
('4299'),
('42999'),
('429998');

SELECT
    P.prefixo,
    min(P.prefixo - Q.pos)
FROM    
    prefixos P
    LEFT JOIN (
        SELECT  prefixo,
            substring('4299234180' from 0 for char_length(prefixo::varchar)+1)::integer as pos
        FROM    prefixos
    ) as Q ON Q.prefixo = P.prefixo
WHERE
    (P.prefixo - Q.pos) > 0
GROUP BY
    P.prefixo
ORDER BY
    2
LIMIT   1

Explanation

To solve your problem I used the following logic:

  • Capture the part of the number corresponding to the size of the prefix. Prefix 429 has len = 3, so I'll capture the first 3 characters of your number.

The part of does this is:

LEFT JOIN (
    SELECT  prefixo,
        substring('4299234180' from 0 for char_length(prefixo::varchar)+1)::integer as pos
    FROM    prefixos
) as Q ON Q.prefixo = P.prefixo
  • After having the corresponding number to the prefix, I perform an arithmetic operation to remove them equal to zero because it wants to say that a higher prefix should fit.
  • Finally, I check as little variation as possible so that I have the prefix that fits best. (P.prefixo - Q.pos) > 0

Note

  • The base I used was PostgreSQL, you may need to adapt something, as not all DBMS have the same functions.
22.03.2017 / 14:58