Skip to content

Instantly share code, notes, and snippets.

View xexi's full-sized avatar

Hyper xexi

  • DMC
View GitHub Profile
@xexi
xexi / js extract file ext
Last active July 31, 2017 07:59
js extract file ext
var test = 'myfile.txt';
console.log(test.match(/\.[0-9a-z]+$/i)[0]);
@xexi
xexi / extract YMD from different date format
Last active July 20, 2017 01:22
extract YMD from different date format
//var date1 = '20170602110357';
//var date2 = '2016-05-13 04:06:10'
//var date3 = '2014-02-27T22:00:00.000Z'
//var date4 = '2016/03/22 23:11:06'
function escapeRegExp(s) {
return s.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceAll(s, find, replace) {
@xexi
xexi / checkNested.js
Created July 18, 2017 06:41
javascript-test-for-existence-of-nested-object-key
function checkNested(obj /*, level1, level2, ... levelN*/) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
@xexi
xexi / mongo_shell_distinct
Last active July 18, 2017 06:42
mongo_shell_distinct
db.collection.aggregate( [
{ $match: { 'dept': "service", 'condition': '0' } },
{ $group: { _id: { first: "$distinct_col1", second: "$distinct_col1" }, // distinct option
id_tag : { $min: "$tag" }, // projection
count: { $sum:1 } // group count
}
},
{ $sort: { '_.id.regdate': 1 }}, // make sure sort by group id // 1 asc
{ $skip: 0 },
{ $limit: 5 },
db.collection('my_collection').find( { 'findData': 'MorefindOption',
$or : [
{ 'name' : { $regex: 'searchWord' , $options: 'ix' } },
{ 'adress' : { $regex: 'searchWord' , $options: 'ix' } }
] })
// more option available at google search -> mongodb regex option
// -> in mongo cli { name : /.*searchWord.*/i }
var n = 5;
var nn = [n];
for (var i = 1; i < 5; i++) {
nn.push(nn[0] - i);
}
console.log(JSON.stringify(nn));
db.collection.find({ "findData": "blabla" }).sort({"orderData": -1}).limit(1)
db.collection('myCollection').find( { 'findData': { $exists: true } }).toArray( (err, results) => {});
var arr = [];
// for any falsy value (0, "", NaN, null, undefined, or of course, false):
if (!myarray[0]){
// isEmpty
} else {
// notEmpty
}
let bulk = db.collection('myCollection').initializeOrderedBulkOp();
for(let i=0; i < arr.length; i++){
bulk.find( {findData: arr[i]} ).upsert().updateOne(
{
$set: { modData: 'yolo' }
}
);
}