2020-04-30

MongoDB - How to create a "Not and" filter

db.collection_name.find({
  "other_field": "some_value"

  , "$nor": [{ 
    "$and": [
      {"field_filter_1": "value_1"}
      , {"field_filter_2": "value_2"}
    ]
  }]

});

JavaScript - Find a children object by key/value using Underscore.js

let child_object
   _.find(
     parent_object
     , function (o) { 
       return o.key_to_search_by === value_searched
     }
   );

2020-04-27

MongoDB - Filter for documents dated in the last 30 days

db.collection_name_here.find({
    "date_field_name_here": { 
        $gte: new Date(
            (
                new Date().getTime() 
                - (30 * 24 * 60 * 60 * 1000)
            )
        ) 
    },
});

2020-04-26

MongoDB - IF statement using field regex match condition

"field_name_here":
    {"$cond": [
        {$regexMatch: {
            input: "$source_field_name_here",
            regex: /string_to_match_here/i
        }}
        , NumberInt(1) //value if matched
        , NumberInt(0) //value if not matched
    ]},

2020-04-25

jQuery - Ajax call example

<!DOCTYPE html>
<html>
<head>
<script
  src="https://code.jquery.com/jquery-1.12.4.min.js"
  integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
  crossorigin="anonymous"></script>
</head>
<body>

<script>
$.ajax({
url: "https://jsonplaceholder.typicode.com/todos/1",
beforeSend: function(xhr) {
//xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
xhr.setRequestHeader("Content-Type", "application/json")
},
type: "POST",
dataType: "json",
contentType: "application/json",
processData: false,
data: '{"foo":"bar"}',

success: function (response) {
alert(JSON.stringify(response));
},

error: function(){
alert("Error!");
}
});
</script>


</body>
</html>

Meteor - How to install npm packages

meteor npm install --save package_name_here

2020-04-23

MongoDB - Find documents with _ids of the ObjectId type


db.collection_name.find( 
    {"_id": {$type: "objectId"}} 
);

2020-04-20

Python - How to determine the default/active version on your profile

(1) Command-line method:

    python --version



(2) Code method:

    import sys
    print(sys.version)


2020-04-13

MongoDB - Starts with string search

db.collection_name.find({ "field_name" : /^string_here/ });

2020-04-06

jQuery - Scroll to first element of a given class or selector

$('html, body').animate(
  {
    scrollTop: $('CSS_SELECTOR').first().offset().top
  }
  , 750
);