Radoslav Toth
June 29, 2022

MongoDB NodeShell

Mingo's NodeShell let's you execute JavaScript code to work with data directly in Mingo. 
This is NOT MONGO SHELL, though.

The command must be a regular async JS function with a return value.

This allows complex code with loops, conditions and variable initialization to be executed. This code must be a definition of a async function and should return something to see "results". You can use this to fetch and process data using JS code and return the result or update documents using a script with loop, etc. Examples:

async function run() {
  return await db.collection("").find({});
}
async function run() {
  const docs = await db.collection("Amenities").find({}).toArray();
  for (const doc of docs) {
    await db.collection("Amenities").updateOne({_id: doc._id}, {$set: {something: "new"}})
  }

  return docs.length;
}
async function run() {
  const oldSlug = await MingoPrompt('What was the old slug?')
  const newSlug = await MingoPrompt('What will be the new slug?')
  const confirmed = await MingoConfirm('You want to change ' + oldSlug + ' to ' + newSlug + '?');
  if (!confirmed) return false;

  const result = {
    'Locations': await db.collection("Locations").update({slug: oldSlug}, {
      "$set": { "oldSlug": oldSlug, "slug": newSlug }
    }),
    'Attractions': await db.collection("Attractions").updateMany({locationsCache: oldSlug}, {
      "$set": { "locationsCache.$": newSlug }
    }),
  }
  return result;
}

Variables available in the function's scope

You can use the following variables in your code:

  • db: the current MongoDB database connection
  • ObjectId: MongoDB wrapper for creating and manipulating ObjectIds
  • dayjs: for work with dates
  • _: lodash
  • MingoConfirm(message): Simple confirm modal shown with specified message. Returns true if user clicked on OK and false on Cancel.
  • MingoAlert(message): Simple alert modal shown with specified message. 
  • MingoPrompt(message): Simple prompt modal shown with specified message. Returns the value the user entered.

History

NodeShell keeps track of all previous functions executed so you can return to any of them later. Once you have a long list of history, you may search for any function using a fulltext search.

Snippets and saved code

Mingo provides some example snippets. You may also save your code for later use. Code is saved for each database separately.

Read more

Compare and sync entire MongoDB databases 

Do you ever need to download the production DB to your localhost for development purposes? Or just sync changes from one DB to another? This can be done using Mingo’s Compare & Sync tool. You don’t need to write scripts or use the terminal mongodump / mongorestore functions, anymore. With Mingo’s Compare & Sync tool […]
Radoslav Toth

Mingo’s keyboard shortcuts

We are all tired of new shortcuts we have to learn. Mingo’s shortcuts mimic what you are already used to from a browser or your dev tool. However, Mingo also has some new intuitive “hovering” shortcuts. Just hover over a document and press space or X to select the document. Or hover a document and CMD+C / CTRL+C to copy document value.  To […]
Radoslav Toth

Smart search

The majority of queries we execute on MongoDB collections are very simple. We usually search for an _id, an email or username and we do this many times over every day. To make such searches simple and fast, Mingo provides a few great hacks for you. Value as query First of all, indexed fields can […]
Radoslav Toth

Data relations in Mingo

When documents reference each other, they usually do so by the _id of the referenced document. There are several types of document relations (such as one-to-many, one-to-one, many-to-one, many-to-many), but we can generally say that there is always a parent document (the one referencing) and a child document (the one being referenced). For example, you […]
Radoslav Toth

5 best MongoDB GUI in 2022

What is a MongoDB GUI client? MongoDB GUI is a graphical interface for MongoDB. It provides a way to view and manipulate data in MongoDB databases using the MongoDB API or MongoDB Shell. As you may know, you can query your data through the command line, but this gets messy and you can get easily […]
daviddurika

MongoDB aggregations for beginners

MongoDB's aggregation is one of the most powerful tools in the database, allowing you to perform complex data analysis on large datasets. This article will show you how to use aggregation to create powerful data aggregations. Basic aggregations An aggregation is a powerful tool for data analysis. It allows you to perform complex queries on […]
daviddurika

Download Mingo now

The best MongoDB GUI Admin. Intuitive • Fast • Secure
Try for Free