This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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} ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 : [] } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var headerInfo = "<p>"; | |
for(var header in req.headers) { | |
headerInfo += header + " = " + req.headers[header] + "<br />"; | |
} | |
headerInfo += "</p>"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
NewerOlder