db.COLLECTION_NAME.find(
{ FIELD_NAME : { $exists: true}}
);
2019-05-09
MongoDB - Retrieve documents where a field does not exist
db.COLLECTION_NAME.find(
{ FIELD_NAME : { $exists: false}}
);
{ FIELD_NAME : { $exists: false}}
);
2019-05-03
MySQL - RegEx replacements
-- This may only work with certain versions of MySQL (v8+ possibly)
-- These functions do not work on select statements using a DISTINCT clause
SELECT REGEXP_REPLACE('a b c', 'b', 'X');
SELECT REGEXP_REPLACE('a b c', '[a-z]', 'X');
SELECT REGEXP_REPLACE('a b c 123 cuando ', '[[:alpha:]]', 'X');
SELECT REGEXP_REPLACE('a b c 1987-2087 cuando ', '[[:alpha:]]', '');
SELECT REGEXP_REPLACE('a b c 1987-2087 cuando ', '[[:alpha:]]', '', 1, 0, 'i');
SELECT REGEXP_REPLACE(
author
-- , '[a-z]+' -- Regex Pattern
, '[[:alpha:]]'
, 'X' -- Replace value
, 1 -- Starting position
, 1 -- Occurrence
, 'i' -- Match type (i=case insensitive, c=case sensitive, m=multiple line mode, u=unix-only line endings)
);
-- These functions do not work on select statements using a DISTINCT clause
SELECT REGEXP_REPLACE('a b c', 'b', 'X');
SELECT REGEXP_REPLACE('a b c', '[a-z]', 'X');
SELECT REGEXP_REPLACE('a b c 123 cuando ', '[[:alpha:]]', 'X');
SELECT REGEXP_REPLACE('a b c 1987-2087 cuando ', '[[:alpha:]]', '');
SELECT REGEXP_REPLACE('a b c 1987-2087 cuando ', '[[:alpha:]]', '', 1, 0, 'i');
SELECT REGEXP_REPLACE(
author
-- , '[a-z]+' -- Regex Pattern
, '[[:alpha:]]'
, 'X' -- Replace value
, 1 -- Starting position
, 1 -- Occurrence
, 'i' -- Match type (i=case insensitive, c=case sensitive, m=multiple line mode, u=unix-only line endings)
);
2019-05-02
MySQL - Split string into pieces delimited by another string, and retrieve only the 'nth' element
select
-- Original field value
original_value
-- Second element, without delimiter
, REPLACE(
-- Second element, including its delimiter
REPLACE(
-- Desired element with all previous ones
SUBSTRING_INDEX( original_value, ',', 2)
-- All previous elements
, SUBSTRING_INDEX( original_value, ',', 1)
, ''
)
, ','
, ''
) as _second_element
-- Fourth element
, REPLACE(
-- Desired element with all previous ones
SUBSTRING_INDEX( original_value, ',', 4)
-- All previous elements
, SUBSTRING_INDEX( original_value, ',', 3)
, ''
) as _fourth_element
from
(select 'apple jelly and toast,bbq chicken,celery logs,daisies-bouquet,ebony,f-key,g-string,ivory' as original_value) as n1
;
-- Original field value
original_value
-- Second element, without delimiter
, REPLACE(
-- Second element, including its delimiter
REPLACE(
-- Desired element with all previous ones
SUBSTRING_INDEX( original_value, ',', 2)
-- All previous elements
, SUBSTRING_INDEX( original_value, ',', 1)
, ''
)
, ','
, ''
) as _second_element
-- Fourth element
, REPLACE(
-- Desired element with all previous ones
SUBSTRING_INDEX( original_value, ',', 4)
-- All previous elements
, SUBSTRING_INDEX( original_value, ',', 3)
, ''
) as _fourth_element
from
(select 'apple jelly and toast,bbq chicken,celery logs,daisies-bouquet,ebony,f-key,g-string,ivory' as original_value) as n1
;
MySQL - Split string into groups and retrieve 'n' number of groups
select
SUBSTRING_INDEX( v, ',', 1) as first_part
, SUBSTRING_INDEX( v, ',', 2) as first_and_second_parts
, SUBSTRING_INDEX( v, ',', 3) as first_through_third_parts
, SUBSTRING_INDEX( v, ',', 4) as first_through_fourth_parts
, SUBSTRING_INDEX( v, ',', 5) as first_through_fifth_parts
, SUBSTRING_INDEX( v, ',', 0) as no_parts
, SUBSTRING_INDEX( v, ',', 100) as all_parts
from
(select 'apple, bbq, celery, daisies, ebony' as v) as n1
;
SUBSTRING_INDEX( v, ',', 1) as first_part
, SUBSTRING_INDEX( v, ',', 2) as first_and_second_parts
, SUBSTRING_INDEX( v, ',', 3) as first_through_third_parts
, SUBSTRING_INDEX( v, ',', 4) as first_through_fourth_parts
, SUBSTRING_INDEX( v, ',', 5) as first_through_fifth_parts
, SUBSTRING_INDEX( v, ',', 0) as no_parts
, SUBSTRING_INDEX( v, ',', 100) as all_parts
from
(select 'apple, bbq, celery, daisies, ebony' as v) as n1
;
Subscribe to:
Posts (Atom)