Skip to content

Instantly share code, notes, and snippets.

@guyellis
guyellis / gist:6922284
Created October 10, 2013 17:29
How to convert an IEnumerable to a dictionary using LINQ
// If you want to convert an IEnumerable to a dictionary using LINQ then do:
Dictionary<string, object> dict = myList.ToDictionary(a => a.MyString, a => a.MyObject);
@guyellis
guyellis / gist:6928951
Created October 11, 2013 03:01
Put HTTP headers into HTML <p> element when using NodeJS and Express route.
var headerInfo = "<p>";
for(var header in req.headers) {
headerInfo += header + " = " + req.headers[header] + "<br />";
}
headerInfo += "</p>";
// suggested shell cmd line to run this:
//
// mongo --shell example2.js
//
// Note: the { out : … } parameter is for mongodb 1.8+
db.things.insert( { _id : 1, tags : ['dog', 'cat'] } );
db.things.insert( { _id : 2, tags : ['cat'] } );
db.things.insert( { _id : 3, tags : ['mouse', 'cat', 'dog'] } );
db.things.insert( { _id : 4, tags : [] } );
@guyellis
guyellis / removeMongoField.js
Last active November 30, 2021 23:37
How to remove a field from a collection in MongoDB
// Params:
// 1. query (filter) if you only want to remove this field from some records. {} will select any.
// 2. $unset the field(s) that you want to remove. Here I've set them to null but the value doesn't matter as it's ignored.
// 3. multi must be set to true otherwise it will only operate on the first record that it finds.
//
// Run this command in the MongoDB shell
db.<collectionName>.update( {}, {$unset: {<fieldName1>: null, <fieldName2>: null}}, {multi: true});
@guyellis
guyellis / renameMongoField.js
Last active November 30, 2021 23:36
How to rename a field in all documents in a collection in MongoDB
// Params
// 1. {} add a query in here if you don't want to select all records (unlikely)
// 2. $rename one or more fields by setting the keys of the object to the old field name and their values to the new field name.
// 3. Set multi to true to force it to operate on all documents otherwise it will only operate on the first document that matches the query.
//
// Run this in the MongoDB shell.
db.<collectionName>.update( {}, { $rename: { '<oldName1>': '<newName1>', '<oldName2>': '<newName2>' } }, {multi: true} )
@guyellis
guyellis / duplicateMongoDocuments.js
Created April 3, 2014 04:17
How to duplicate the records in a MongoDB collection
// Fill out a literal array of collections you want to duplicate the records in.
// Iterate over each collection using the forEach method on the array.
// For each collection get (find) all the records and forEach on each of them.
// Remove the _id field from each record as MongoDB will generate this for you and
// you can't have a duplicate.
// Save the record.
// This assumes that the collection does not have any unique keys set on any of the
// other fields.
// Run this in the MongoDB shell
// Demonstrates to anyone who has any doubts that
// lodash remove() will not procede (i.e. execute
// synchronously) until it has a result.
var _ = require('lodash');
var x = [1,2,3,4,5,6,7,8,9];
console.log('a1: ' + a);
var a = _.remove(x, function(item){
@guyellis
guyellis / parseEmailsFromString.js
Created May 1, 2014 16:34
Allows a list of space,comma,semicolon,tab and newline delimited emails to be parsed into an array.
// Accepts a single paramater which is one or more emails separated
// by space,comma,semicolon,tab, or newline.
// Returns an array of tokens that should be emails
// Does not validate emails to see if they are well formed.
exports.parseEmails = function(emails) {
return emails.toLowerCase().split(/[\s,;\t\n]+/);
};
@guyellis
guyellis / namegen1.js
Created June 5, 2014 18:43
Name generators
var vowels = ['a','e','i','o','u'];
var alphabet = 'bcdfghjklmnpqrstvwxyz';
for(var a=0; a<alphabet.length; a++) {
for(var v1=0; v1<vowels.length; v1++) {
for(var b=0; b<alphabet.length; b++) {
for(var c=0; c<alphabet.length; c++) {
for(var v2=0; v2<vowels.length; v2++) {
for(var d=0; d<alphabet.length; d++) {
console.log(alphabet[a] + vowels[v1] + alphabet[b] + alphabet[c] + vowels[v2] + alphabet[d]);
}
@guyellis
guyellis / gist:2f9d6afdad8f44038eee
Created September 25, 2014 18:44
npm-debug.log
0 info it worked if it ends with ok
1 verbose cli [ 'node', '/home/guy/local/bin/npm', 'publish' ]
2 info using npm@2.0.0
3 info using node@v0.10.31
4 verbose publish [ '.' ]
5 verbose cache add [ '.', null ]
6 verbose cache add spec="." args=[".",null]
7 verbose parsed spec { raw: '.',
7 verbose parsed spec scope: null,
7 verbose parsed spec name: null,