I just updated mesh - MongoDB Extended Shell again by adding a function that you can call on MongoDB collections.

The new function is called insertArray().

It allows you to insert an array of objects into a collection using a shell version of Syntactic Sugar.

All it does is loop through the array that was passed in, calling DBCollection.insert() on each object in the array.

If an item in the array is not an object, it will skip the insert, print a warning, and continue processing the next item in the array.

If you don't pass in a valid array, it will throw an error.

Example uses are:

// insert 2 items into myCollection
var myArray = [
  { _id: 1, test: 1 },
  { _id: 2, test: 'foo' },

// transfer 10 items from collection1 into collection2

Below is the function definition. Rather than using polyfills, it relies underscore.js (which is included in mesh.js).

/*jslint nomen: true, plusplus: true */
/*global _, DBCollection, print */
 * @function
 * @name insertArray
 * @memberOf DBCollection
 * @param {array} arr - The array of objects to insert.
 * @param {object} options - pass through to DBCollection.prototype.insert()
 * @param {boolean} _allow_dot - pass through to DBCollection.prototype.insert()
 * @throws {Exception} - when arr is not an Array.
DBCollection.prototype.insertArray = function (arr, options, _allow_dot) {
  'use strict';
  var i, obj;
  if (_.isArray(arr)) {
    for (i = 0; i < arr.length; i++) {
      obj = arr[i];
      if (_.isObject(obj) && !_.isFunction(obj)) {
        this.insert(obj, options, _allow_dot);
      } else {
        print('Cannot insert a non-object, so skipping: ' + obj);
  } else {
    throw 'first argument is not an array!';
Tagged with: MongoDBShellShell ExtensionsProjectsUpdate