2022-08-17

Meteor - How to dynamically load the Mongo Charts JavaScript library

 if(false) 

    import("@mongodb-js/charts-embed-dom");


const { default: ChartsEmbedSDK } = await import(

    `@mongodb-js${'/'}charts-embed-dom`

);


2022-07-04

MongoDB - Remove the timestamp from a mongo date

 db.collection_name.aggregate([    

    {$addFields: {

        "date_with_uniform_time": {$dateTrunc: {

              date: "$source_date_field",

              unit: "day",

              timezone: "America/New_York",

           }},

    }},

    {$addFields: {

        "date_without_time": {$substr: [

            "$date_with_uniform_time", 

            0, 10

        ]}

    }},

]);


2022-06-19

Snowflake - How to enable the customer support feature, and grant access to it

// Create user support role 
// (required to receive support, un-be-lie-va-ble)

USE ROLE ACCOUNTADMIN

CREATE ROLE USER_SUPPORT

GRANT MANAGE USER SUPPORT CASES ON ACCOUNT TO ROLE USER_SUPPORT


// Grant access to their own cases

USE ROLE ACCOUNTADMIN

GRANT ROLE USER_SUPPORT TO USER USER_NAME_NO_QUOTES;


// Grant access to all access in the account

USE ROLE ACCOUNTADMIN

CREATE ROLE ACCOUNT_SUPPORT;

GRANT MANAGE ACCOUNT SUPPORT CASES ON ACCOUNT TO ROLE ACCOUNT_SUPPORT;


// Grant access to all the organization's cases 

USE ROLE ORGADMIN

CREATE ROLE ORGANIZATION_SUPPORT;

GRANT MANAGE ORGANIZATION SUPPORT CASES ON ACCOUNT TO ROLE ORGANIZATION_SUPPORT;


Snowflake - How to retrieve your current account name

select current_account();

Snowflake - How to retrieve the currently logged in user name

select current_user();

2022-06-15

MongoDB - How to insert an "ObjectId" value manually

 db.collection.insert({

    "_id": {

        "$oid": "32b8d4ebe3fa527c849345e7"

    },

    "field_2": "value_2"

});

2022-04-08

MongoDB - Match based on multiple keys of an array of objects

 db.collection_name.aggregate([

    {$match: {

        "array_of_objects": {

            $elemMatch: {

                "object_property_1": value_1,

                "object_property_2": value_2

            }

        }

    }},

]);


MongoDB - Match based on multiple keys of an array of objects NOT having the given values

 db.collection_name.aggregate([

    {$match: {

        $nor: [

            {"array_of_objects": {

                $elemMatch: {

                    "object_property_1": value_1,

                    "object_property_2": value_2

                }

            }}

        ]

    }},

]);

2022-04-02

MongoDB - Filter array

 db.collection_name.aggregate([

    {$addFields: {

        list: {$filter: {

            input: '$array_field_name',

            as: 'item',

            cond: {

                $gt: [

                    '$$item.array_subfield_name', 

                    value

                ]

            }

        }}

    }}

]);


2022-03-27

MongoDB - New alternative update syntax in Mongo 5

 db.runCommand({

   update: "collection_name",

   updates: [

       {

           q: {

               "field1": "value1",

               "field2": "value2",

           },

           u: {

               $set: {

                   "field3": "value3",

               },

               multi: false,

               upsert: false,

           }

       }

   ],

});


MongoDB - Filter documents where a given field is NOT an array

 db.collection.aggregate([

    {$match: {

         $expr: { $ne: [ {$isArray: "$field_to_check"}, true] }

    }}

]);   

MongoDB - Filter when the result of two AND conditions, is NOT true (not -> X and Y)

db.collection_name.find({

    $nor: [

        {$and: [

            {"field1": "value"},

            {"field2.subfield": "value"},

        ]}

    ]

});   

2022-03-07

MongoDB - Update all collection documents with a new copy of an existing field

 db.COLLECTION_NAME.update(

    {

        FIELD_1: ANY_VALUE

    },

    [

        {$set: {

            "NEW_FIELD_COPY": "$ORIGINAL_SOURCE_FIELD", 

        }},

    ],

    {

        multi: true, 

        upsert: false,

    }

);