Skip to content

MongoDB

  • Action
  • Databases

Finds, inserts, updates or deletes documents in a MongoDB collection. Find hands them over as JSON for For Each or Read JSON, such as today's orders; Insert adds one document or a JSON list of them; Update changes the first document that matches the filter, or every one; Delete deletes them. Continues on Failed with the reason in Error when the server can't be reached or refuses it, such as a duplicate _id.

The MongoDB step in the MacroWin editor
Ctrl+Alt+T finds the newest open helpdesk tickets assigned to Priya and saves them to a file on the desktop.

Settings

SettingStarts asWhat it does
Operation Find Find only reads; Insert, Update and Delete change the collection.
  • Find: Finds documents and hands them over as JSON.
  • Insert: Adds one document, or a JSON list of them.
  • Update: Changes the first document that matches the filter, or every one.
  • Delete: Deletes the first document that matches the filter, or every one.
Server ${} localhost The MongoDB server's name or IP address, such as localhost or 192.168.1.10. Not used when Connection string is filled in.
Database * ${} empty For example, shop.
Collection * ${} empty For example, orders.
Filter empty Which documents, as JSON, such as {"status": "@status"} or {"total": {"$gt": "@min:number"}}. {} matches every document; so does an empty Filter, except for Delete, which needs one. It runs as typed; MacroWin values go in Parameters and are named here as "@name".
Documents * empty One document, such as {"name": "@name", "total": "@total:number"}, or a list [{...}, {...}]. It runs as typed; MacroWin values go in Parameters and are named here as "@name".
Changes * empty Update operators, such as {"$set": {"status": "@status"}, "$inc": {"visits": 1}}; with Replace the whole document, the new document. It runs as typed; MacroWin values go in Parameters and are named here as "@name".
Parameters ${} empty One per line as name = value, such as customer = ${name}. The query names one as "@customer" (text), or "@age:number", "@paid:bool", "@since:date", "@id:id" (an ObjectId). The value goes in as a value of its own, so quotes or operators in it can't change the query.
Sort empty The order, such as {"date": -1} for the newest first. Empty: the server's own order. It runs as typed; MacroWin values go in Parameters and are named here as "@name".
Fields empty Which fields to return, such as {"name": 1, "total": 1}. Empty: all of them. It runs as typed; MacroWin values go in Parameters and are named here as "@name".
Limit 100 At most this many documents. 0 returns every one, which can be a lot.
Every matching document Off Off: only the first that matches. For Delete, on with the filter {} deletes every document in the collection.
Insert when nothing matches Off On: a new document is made from the filter and the changes when none matches.
Replace the whole document Off On: Changes is the new document, which takes the matching one's place (keeping its _id).
Connection string ${} empty Instead of the server fields: mongodb://... or mongodb+srv://... as MongoDB Atlas or Compass copies it. Stored as a password, since it usually holds one.
Port 27017 Usually 27017.
User name ${} empty Leave it empty for a server without sign-in. Signs in against the admin database.
Password ${} empty The user's password. Stored as a password.
TLS Off
  • Off: Not encrypted: for a server on this PC or your own network.
  • On: Encrypted; the server's certificate must be one Windows trusts. Atlas and most hosted servers use it.
  • On, accept the server's own certificate: Encrypted, and any certificate is accepted, such as a self-signed one on a home server. Someone who can intercept the connection could pose as the server.
Time limit (ms) 10000 For reaching the server and the operation. 0 waits as long as it takes.

Settings marked ${} take variables. * is required.

Where it continues

  • Out when it's done.
  • Failed when the server can't be reached, refuses the sign-in or the command, or doesn't answer in time; the reason is in Error.

Outputs

MongoDB outputs
DocumentsOne document per line, each as JSON, for For Each.
JSON listAll documents as one JSON list, for Read JSON (items[0].name).
FirstThe first document as JSON; empty when none was found.
CountFind: how many were found. Insert: how many were inserted.
IDsEach new document's _id, one per line (an ObjectId as its 24 hex digits).
IDInsert: the first new document's _id. Update: the new document's _id when Insert when nothing matches made one; empty otherwise.
MatchedHow many documents matched the filter.
ChangedHow many were changed (a document that already had the values isn't), with one inserted counted.
DeletedHow many documents were deleted.
ErrorWhy it failed; empty when it worked.

More in Databases: Redis, Run SQL

Build macros with MongoDB in MacroWin. Free to download, no account needed. Windows 10 and 11.

Download