2023-12-29

MongoDB - Remove a field from an array of documents

 db.collection_name.updateOne(

   {"array_of_docs.field_name": {$exists: 1}}, 

   {$unset: {"array_of_docs.$[].field_name": ""}}

);

2023-11-11

2023-10-24

MongoDB - Update a field using the value of another field

db.COLLECTION_NAME.updateMany(

{},

[{$set: {

"target_field": "$source_field"

}}]

);

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 '$'


2023-01-18

MongoDB - Count the number of fields or keys in a sub-document

 db.COLLECTION_NAME.aggregate([

    {$addFields: {

        'SUB_DOCUMENT_FIELD_COUNT': 

        {$size

        {$objectToArray

        {$ifNull[

                                    "$YOUR_FIELD", 

                                    {}

        ]}

        }

        },

}},

]);

MongoDB - Count the number of fields or keys in a document

 db.COLLECTION_NAME.aggregate([

    {$addFields: {

        'THIS_DOCUMENT_KEY_COUNT': {$size: {$objectToArray: "$$ROOT"}},

}},

]);

2023-01-08

Python - How to create a dataframe with a matrix of random numbers

 import pandas as pd

import numpy as np

from tabulate import tabulate


# 100 rows, 3 columns, random numbers

df = pd.DataFrame(

        np.random.rand(100, 3),

        columns=['COLUMN_1', 'COLUMN_2', 'COLUMN_3']

    )


# Pretty-print it

print(tabulate(df, headers='keys', tablefmt='grid', showindex='always'))




2023-01-07

Python - Insert Pandas dataframe row at the end

 df.loc[len(df.index)] = YOUR_LIST

Pymongo - Insert current timestamp as ISODate

from pymongo import MongoClient

import datetime



client = MongoClient("mongodb://YOUR_CONNECTION_STRING")

db = client.YOUR_DATABASE_NAME

collection = db.YOUR_COLLECTION_NAME


# Truncate

collection.delete_many({})


# Date treatment

date = datetime.datetime.utcnow()

at = datetime.datetime.strptime(

    str(date),

    "%Y-%m-%d %H:%M:%S.%f"

)


# Insertion

collection.insert_one({"YOUR_FIELD_NAME": at})


2023-01-03

Python - How to unite two dataframes

 import pandas as pd


df1 = pd.DataFrame()

df2 = pd.DataFrame()

df3 = pd.concat([df1, df2])