mongo/mongoose query to find field removing space - angularjs

Can we create a mongoose query to return a result of a specific field data without space in between?
for example:
series 'KA 04 A' and 'KA04A' is same. i want to make a check while adding a new series if the new series is already exist in my mongodb thru mongoose.
Currently my series field have space in between
the current code of my mongoose is:
seriesModel.find({series_name:req.body.seriesName.toUpperCase(), status:'active'}, function(err, data){
if(err){
logger.info(err);
return false;
}
else if(data.length){
return res.json({error:true,message:'Series name already exists.'});
}
how can i return field data from db without space. so that i can make a check if the same series exists or not.

Ideally you would standardize your series_name field before saving the record, using a custom hook that strips out whitespace.
That being said, you can use a regular expression in your query. For example, you can search for uppercase/lowercase variations on the word "mongoose" as follows:
model.findOne({name: /mongoose/i}, fn(){...});
One way to solve your problem would be to take your string, split it into individual characters, and then inject the whitespace pattern \s between all the characters (I am assuming there is no leading or trailing whitespace in this field):
"^" + "KA04A".split("").join("\\s*") + "$" // "^K\s*A\s*0\s*4\s*A$"
(The start ^ and end $ are necessary so that we do not get false positives on e.g. "xKA04Ax")
Next, you would need to convert this string into the regex format ( /^K\s*A\s*0\s*4\s*A$/) and plug it into your query:
model.findOne({name: new RegExp("^K\\s*A\\s*0\\s*4\\s*A$")}, fn(){...});
Major Caveat: you will need to be super careful that your original string only contains a-zA-Z0-9 and whitespace. It cannot contain anything that could be mistaken as a regex pattern without first escaping those characters (see here for more on this approach).

Related

Postgresql Full text search for numbers preceeded by hashsign

The documents that I want to run full text search on contains sequences of a hash sign followed by a series of digits e.g. #12345 #9999. None of the parsers seem to recognize the sequence as a single token.
The blank parser does recognize '#' as a token, so I thought I could use a synonym dictionary to match '#' with 'num' and then use the follows operator e.g. # <-> 1234. However; the blank parser groups all the blank character into one token so the token usually contains a leading space ' #'. I can't make a synonym entry with a leading space (or at least don't know how to).
If I included the english_stem dictionary in the mapping for the blank parser then ' #' is recognized as a lexeme. But so are all the other blank characters which adds too much noise to the generated ts_vector
Short of creating a custom parser is there anyway I can configure the search so that I can use full text search to query explicitly for #0000 patterns?

Regex pattern matching for input type time

To check whether input type "time" field is completed (09:00am) I have used a regular expression.
ng-pattern="\b((1[0-2]|0?[1-9]):([0-5][0-9]) ([AaPp][Mm]))"
But in the same regular expression I want to check whether the input field is empty. For further information, time field can be empty or completed (ex: )(09:30am)
Can anyone help me regarding this..
In an ng-pattern, you need to use
ng-pattern="/^(?:(?:1[0-2]|0?[1-9]):[0-5]\d\s*[AaPp][Mm])?$/"
and if you need to avoid leading/trailing spaces, also add ng-trim="false".
See this regex demo.
The (?:...)? optional non-capturing group is a wrapper for the whole pattern that becomes optional, i.e. can match an empty string.
The ^ anchor will only match at the start of the string, and $ will anchor the match at the end of the string, so that an entire string should match.
In case somebody is looking for a regex for European time format (00:00-23:59), as i was, here is the regex for that:
^(?:1[0-9]|2[0-3]|0?[0-9]):[0-5]\d{1}?$
Hope this helps somebody save a minute or two.

trim characters from string of one field and fill a new field for each record in SQL table

I added a new field "StrikePrice" to my existing table "OS1115".
I need to trim some characters from an existing field "symbol" and enter that into the new field.
I know I can do this:
UPDATE OS1115 SET StrikePrice = RIGHT(symbol, 4)
But the problem is that the amount of characters I need vary in length, but are always superseded by either a P or a C.
Here are a couple examples:
QQQ_112015C112.5
PCLN_112015P1287.5
NFLX_112015P107
I need to trim the numbers at the end of the string that come after the P or C and enter that in the new field.
so in this case that would result in:
112.5
1287.5
107
How can I do this?
I use MS SQL Express 2012
This would work:
RIGHT(symbol, PATINDEX('%[cp]%', REVERSE(symbol))-1)
though I'm sure there are a variety of ways to do it.
You got one answer while I was testing mine. Here's another way:
substring(StrikePrice,charindex('C',StrikePrice,6)+charindex('P',StrikePrice,6)+1,99)

select first two characters of values in a concatenated string

I am trying to create a formula field that checks a string that is a series of concatenated values separated by a comma. I want to check the first two characters of each comma separated value in the string. For example, the string pattern could be: abcd,efgh,ijkl,mnop,qrst,uvwx
In my formula I'd like to check if the first two characters are 'ab','ef'
If so, I would return true, else false.
Thanks.
To do this properly, you need to use a regular expression. Unfortunately the REGEX function is not available in formula fields. It is, however, available in formulas in Validation Rules and in Workflow Rules. You can, therefore, specify the below formula in either of a Validation or Workflow Rule:
OR(
AND(
NOT(
BEGINS( KXENDev__Languages__c, "ab" )
),
NOT(
BEGINS( KXENDev__Languages__c, "ef" )
)
),
REGEX( KXENDev__Languages__c , ".*,(?!ab|ef).*")
)
If it's a Validation Rule, you're done -- this formula will create an error if any of the entries do not start with "ab" or "ef". If it's a Workflow Rule, then you can add a Field Update to it to update some field with False when this formula is true (if this formula is true then there is at least one item that doesn't start with ab or ef, so that would make your field False).
Some may ask "What's with the BEGINS statements? Couldn't you have done this all with one REGEX?" Yes, I probably could, but that makes for an increasingly complex REGEX statement, and these are quite difficult to debug in Salesforce.com, so I prefer to keep my REGEXes in Salesforce.com as simple as possible.
I suggest you to search for ',ab' and ',ef' using CONTAINS method. But first of all you need to re implement method which composes this string so it puts ',' before first substring. At the end returned string should look like ',abcd,efgh,ijkl,mnop,qrst,uvwx'.
If you are not able to re implement method which compose this string use LEFT([our string goes here],2) method to check first two chars.

.NET Regex for SQL Server string... but not Unicode string?

I'm trying to build a .NET regex to match SQL Server constant strings... but not Unicode strings.
Here's a bit of SQL:
select * from SomeTable where SomeKey = 'abc''def' and AnotherField = n'another''value'
Note that within a string two single quotes escapes a single quote.
The regex should match 'abc''def' but not n'another''value'.
I have a regex now that manages to locate a string, but it also matches the Unicode string (starting just after the N):
'('{2})*([^']*)('{2})*([^']*)('{2})*'
Thanks!
This pattern will do most of what you are looking to do:
(?<unicode>n)?'(?<value>(?:''|[^'])*)'
The upside is that it should accurately match any number of escaped quotes. (SomeKey = 'abc''''def''' will match abc''''def''.)
The downside is it also matches Unicode strings, although it captures the leading n to identify it as a Unicode string. When you process the regular expression, you can ignore matches where the match group "unicode" was successful.
The pattern creates the following groups for each match:
unicode: Success if the string is a Unicode string, fails to match if ASCII
value: the string value. escaped single quotes remain escaped
If you are using .NET regular expressions, you could add (?(unicode)(?<-value>)) to the end of the pattern to suppress matching the value, although the pattern as a whole would still match.
Edit
Having thought about it some more, the following pattern should do exactly what you wanted; it will not match Unicode strings at all. The above approach might still be more readable, however.
(?:n'(?:''|[^'])*'[^']*)*(?<!n)'(?<value>(?:''|[^'])*)'

Resources