How do I find a partial string in a Mongo database using a superset string? - mongodb

If my database contains entries with the following string values for the "key" field:
"a,b,c"
"a,b,z"
"a,b,c,d,e,f,z"
"d,e,f,g"
"d,e,f,g,z"
"h,i"
And I have a string like this:
"a,b,c,d,e,f,g,h"
How do I find the entries where the value of the key field matches the start of my string? E.g. I want to find the entry where the value of the key field is "a,b,c".
How do I find the entries where the value of the key field matches any part of my string? E.g. I want to find the entries where the value of the key field is "a,b,c" and "d,e,f,g".
To give some context in case anyone thinks this is a pointless task, I want to do stack matching. I will have entries in a database that identify bugs by the first N frames of the stack and then I want to identify bug(s) by the stack obtained from a core dump.

The answer is to use the $where operator. An example in Python, where search_string is the string we want to find matches with, is:
search_string = 'a,b,c,d,e,f,g,h'
js_check = 'function () { var search_string=\'' + search_string + '\'; return search_string.indexOf(this.key) >= 0; }'
matches = my_collection.find({'$where': js_check})

Related

How to interpret indexof expression and functions in Azure Data Factory

I'm trying to understand the indexof expression(function) of Azure Data Factory.
Example
This example finds the starting index value for the "world" substring in the "hello world" string:
indexOf('hello world', 'world')
And returns this result: 6
I'm confused by what is meant by the 'index value' and how the example arrived at the result 6.
Also, using the above example, can someone let me know what would be the answer for the following expression?
#if(greater(indexof(string(pipeline().parameters.Config),'FilenameMask'),0),pipeline().parameters.Config.FilenameMask,'')
indexof
{"FilenameMask":"accounts*."}
'Config' represents a field in sql database
Per the docs:
Return the starting position or index value for a substring. This function is not case-sensitive, and indexes start with the number 0.
hello world
01234567890
^
+--- "world" found starting at position 6
Regarding the 2nd part of your question. Here's the expression re-written for a bit of clarity:
#if( greater(indexof(string(pipeline().parameters.Config),'FilenameMask'),0)
,pipeline().parameters.Config.FilenameMask
,'')
which can be read as follows:
if the index of the string "FilenameMask" within x is greater than 0 then
return x.Filenamemask
else
return an empty string
where x is pipeline().parameters.Config, which is the value of your "Config" column from the database table. It will hold values such as
{"sparkConfig":{"header":"true"},"FilenameMask":"cashsales*."}
and
{"FilenameMask":"accounts*."}
The ADF expression can also be read as follows:
if the JSON in the Config column contains a "FilenameMask" key then
return the value of the FilenameMask key
else
return an empty string

How do I parse out a number from this returned XML string in python?

I have the following string:
{\"Id\":\"135\",\"Type\":0}
The number in the Id field will vary, but will always be an integer with no comma separator. I'm not sure how to get just that value from that string given that it's string data type and not real "XML". I was toying with the replace() function, but the special characters are making it more complex than it seems it needs to be.
is there a way to convert that to XML or something that I can reference the Id value directly?
Maybe use a regular expression, e.g.
import re
txt = "{\"Id\":\"135\",\"Type\":0}"
x = re.search('"Id":"([0-9]+)"', txt)
if x:
print(x.group(1))
gives
135
It is assumed here that the ids are numeric and consist of at least one digit.
Non-regex answer as you asked
\" is an escape sequence in python.
So if {\"Id\":\"135\",\"Type\":0} is a raw string and if you put it into a python variable like
a = '{\"Id\":\"135\",\"Type\":0}'
gives
>>> a
'{"Id":"135","Type":0}'
OR
If the above string is python string which has \" which is already escaped, then do a.replace("\\","") which will give you the string without \.
Now just load this string into a dict and access element Id like below.
import json
d = json.loads(a)
d['Id']
Output :
135

Index and length must refer to a location within the string.?

My input strings are
inputData = "99998UNKNOWN"
inputData = "01000AMEBACIDE/TRICHOM/ANTIBAC 1"
inputData = "34343AMEBACIDE/TRICHOM/ANTIBACSADWA1"
ID = inputData.Substring(0,5);
Name = inputData.Substring(5,30);
Level = inputData.Substring(35,1);
I am getting the below error,
Index and length must refer to a location within the string.
I can understand , the error is due to the length that specified in substring for "Name" is not matching with first input.
Is there any way to handle this issue with any input length?
One approach is to add a "sentinel" suffix to the end of the string before taking substrings. Now you can add it to the data string before taking substrings from it. As long as the suffix has sufficient length, you would never get an index/length exception:
var padded = inputData.PadRight(32);
ID = padded.Substring(0, 5).Trim();
Name = padded.Substring(5, 30).Trim();
Level = padded.Substring(30, 1).Trim();
However, now your code should check if ID, Name, or Level is empty.

get_schema multiple primary keys

I am trying the following:
from pandas.io.sql import get_schema
tbl_schema = get_schema(contracts, 'my_contracts', keys=['country', 'contract_id'], con=db_engine)
I am getting this
ArgumentError: Element ['country', 'contract_id'] is not a string name or column element
which seems likely coming from this:
def _to_schema_column_or_string(element):
if hasattr(element, '__clause_element__'):
element = element.__clause_element__()
if not isinstance(element, util.string_types + (ColumnElement, )):
msg = "Element %r is not a string name or column element"
raise exc.ArgumentError(msg % element)
return element
I am not sure I understand how the multiple primary keys should be formatted to be parsed properly. I don't really understand this: util.string_types + (ColumnElement, ) I was hoping I could just point to the frame columns without having to define the whole SQLAlchemy schema.

jquery syntax to look for a hidden field in a form

I have a form with a table in it. In each row is a table cell with a hidden input item with the name of it starting with "hf_id_" followed by a number so that row 1's field has a name of "hf_id_1", row 2 is "hf_id_2" and so on. I need to search all of these fields for a particular value but I'm not quite sure how to get to the hidden fields. I know how to get to them when the full name is known but in this case I'm not sure if there's a way to get an array of these where name starts with "hf_id_". Thanks.
You can search elements with ^ (starting with) and $ (ending with), example:
$('input[name^="hf_id_"]');
So you can get all those elements like:
var elements = $('input[name^="hf_id_"]');
And you can iterate over them to search for a particular value like:
$('input[name^="hf_id_"]').each(function(){
if ($(this).val() === 'search value here')
{
// found..........
}
});
Or you could simply use
$('input[type="hidden"]');