Skip to content

Instantly share code, notes, and snippets.

// 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 / 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
@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 / 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});
// 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 / 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>";
@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);