2023-10-11

MacOS - Rename extensions for all files in a folder

 for f in *.png; do 

    mv -- "$f" "${f%.png}.jpg"

done

2023-09-11

MongoDB - Replace regex matched string

 db.collection_name.aggregate([

    {$match: {"target_field": {$exists: true}}},

    {$addFields: {

        "remove": {$regexFind: {

            input: "$target_field", 

            regex: /(.){1,}string_to_match/

        }} 

    }},   

    {$match: {"remove": {$exists: true} }},

    {$addFields: {

        "remove_len": {$strLenBytes: "$remove.match"} 

    }},

    {$addFields: {

        "clean": {$substr: [ 

            "$target_field", 

            "$remove_len",  

            100000]} 

        }},

])

2023-09-01

MongoDB - Distinct list of keys in a collection or sub-document

 db.authors.aggregate([

    {$project: {"array": {"$objectToArray": "$sub_doc_field_name"}}},

    {$unwind: "$array"},

    {$project: {

        "key": "$array.k"

    }},

    {$group: {

        _id: "$key",

        count: {$sum: 1},

    }},

    {$addFields: {

        key: "$_id",

    }},

    {$project: {

        _id: 0,

    }},

    {$sort: {

        key: 1,

    }},

]);


2023-08-31

MongoDB - Extract the first year of a string, using RegEx

 db.collection.aggregate([

    {$addFields: {

        "year": {$regexFindAll: {

            input: "$born_string", 

            regex: /\b\d{4}\b/

        }},

    }},

    {$addFields: {

        "year": {$arrayElemAt: ["$year",0]},

    }},

    {$addFields: {

        "year": "$year.match",

    }},

    {$addFields: {

        "year": {$toInt: "$year"},

    }},

]);


2023-03-29

MongoDB - Array of strings to comma-separated string, with no comma after the last element

 db.COLLECTION_NAME.aggregate([

    {$addFields: {

        "CONCATENATED_STRING": {$reduce: {

            "input": "$ARRAY_OF_STRINGS_FIELD_NAME",

            "initialValue": "",

            "in": {

                "$concat": [

                    "$$value",

                    {"$cond": [{"$eq": ["$$value", ""]}, "", ", "]}, 

                    "$$this"

                ]

            }

        }}

    }}    

]);

MongoDB - Array of strings to comma-separated string

 db.COLLECTION_NAME.aggregate([

    {$addFields: {

        "CONCATENATED_STRING": {$reduce: {

            "input": "$ARRAY_OF_STRINGS_FIELD_NAME",

            "initialValue": "",

            "in": {

                "$concat": [

                    "$$value",                    

                    "$$this",

                    ", ",

                ]

            }

        }}

    }}    

]);

2023-02-22

Minimongo - How to cast a date typed as an object, to an actual date type

 db.COLLECTION.update(

    {

        $expr: {$eq: [{"$type": "$YOUR_DATE_FIELD"}, "object"]},

    },

    {$set: {

        "YOUR_DATE_FIELD": new Date(parseInt("$YOUR_DATE_FIELD.$date.$numberLong")),

    }},

    {

        multi: true, 

        upsert: false,

    }

);



Original issue: 

Uncaught MinimongoError: Key $date must not start with '$'