2019-08-28

Python - List of lists to CSV

import csv

# Data
headers = ['Header1', 'Header2', 'Header3']
rows = [[1,2,3], [5,6,7]]

# Open a file to append to
with open('/path/target_file.txt', 'a') as f:

    # CSV writer instantiation
    writer = csv.writer(
    f
    # Settings
    , quoting=csv.QUOTE_NONE
    , delimiter='\t'
    )

    # Headers
    writer.writerow(headers)

    # All rows
    writer.writerows(rows)

2019-08-05

MongoDB - Retrieve only matching sub-documents

db.collection.find(

    // Match
    {
        "subdocument_to_search_within":  {
        "$elemMatch": {
                "field_within_the_subdocument": 100
            }
    }
    , "other_filter": 1
    }

    // Projection
, {
        "subdocument_to_search_within.$": 1
    }
);