2020-12-26

MongoDB - How to update a doubly nested array of objects

 db.COLLECTION.update(

    {

        "any_field": "any_value"

    }

    , {$set: {

        "parent.$[x].child.$[xx].grandchild": "any_value"

    }}

    , {

        arrayFilters: [

            {"x.any_matching_field": "any_value"}

            , {"xx.any_matching_field": "any_value"}

        ]

        , multi: true

        , upsert: false

    }

);

2020-12-22

JavaScript - Regex replace with matches

let str = 'abc def SOMETHING_TO_LOOK_FOR 12';

str = str.replace(

    /(.*)SOMETHING_TO_LOOK_FOR ([0-9]{1,})(.*)/

    , "$1 ANY_OTHER_TEXT $2 $3"

);

console.log(str);


JavaScript - Extract the string between first set of curly brackets

let re = /{([^}]+)}/;

let matches = re.exec(str);

console.log(matches[1]);


2020-12-17

MongoDB - Filter based on sibling fields (2 fields in the same document)

db.COLLECTION.aggregate([   

    {$match: { 

        $expr: {

            $eq: ["$field1", "$field2.subfield3"]

        }

    }}

]);