2020-05-25

MongoDB - Filter string fields by character length

db.collection_name.find({ 
  "$expr": {"$lte": [{"$strLenCP": "$string_field_name"}, 5]} 
});

JavaScript - Aggregate numeric value of an array of objects

// Original array of objects
var objects_array = [
  { id_key: "a", numeric_value: 1 }, 
  { id_key: "b", numeric_value: 2 }, 
  { id_key: "c", numeric_value: 2 }, 
  { id_key: "a", numeric_value: 4 }
];

// Resulting array of objects
var objects_array_aggregated = [];

// Reduction function
objects_array.reduce(function(res, value) {
  if (!res[value.id_key]) {
    res[value.id_key] = { 
        id_key: value.id_key, 
        numeric_value: 0 
      };
    objects_array_aggregated.push(
      res[value.id_key]
    );
  }
  res[value.id_key].numeric_value += value.numeric_value;
  return res;
}, {});

// View the result
console.log(objects_array_aggregated)


Credits:
https://stackoverflow.com/questions/29364262/how-to-group-by-and-sum-array-of-object

2020-05-21

Meteor - List all templates using three bracket kind of outputs

On the client console:

Object.keys(Template).filter(function(key){
  return(
    Template[key] &&
    Template[key].renderFunction && 
    Template[key]
        .renderFunction
        .toString()
        .indexOf('Spacebars.makeRaw') > -1
    );
});


Credits: 
Pete Corey http://www.petecorey.com/blog/2015/04/03/black-box-meteor-triple-brace-xss/

Meteor - List all methods

Once a server session is opened through terminal:

Meteor.default_server.method_handlers

Sublime Text - Remove all line content after a given character

Example for removing anything after the semicolon:

Find:
    \:(.{1,})\n

Replace with:
    \n

2020-05-13

MongoDB - Get the object with the latest date (from an array of objects)

db.collection_name.aggregate([    
  {$project: {


    "output_field_name": {


      $arrayElemAt: [
        "$array_of_objects",
        {
          "$indexOfArray": [
            "$array_of_objects.date_name",
            { "$max": "$array_of_objects.date_name" }
          ]
        }
      ]


    }


  }}  
]);

2020-05-03

MongoDB - IF Statement

"if": {
    "field_name": "value to compare to"
  },
  "then": "value when true",
  "else": "value when false"
}

MongoDB - How to determine if a field is of the date type

"$eq": [{"$type": "$field_name_here"}, "date"]

2020-05-01

JavaScript - Get a string made with a few random characters


              base 36      remove "0."  first few chars

Math.random().toString(36).substring(2).substring(0,4);