Org-mode Babel backend for mongosh.
- Emacs Lisp 99%
- Makefile 1%
| .github/workflows | Feat/variable support ( #1 ) | |
| .gitignore | wip: init | |
| Eask | wip: init | |
| Makefile | Feat/variable support ( #1 ) | |
| ob-mongosh-test.el | Feat/variable support ( #1 ) | |
| ob-mongosh.el | Feat/variable support ( #1 ) | |
| README.org | docs: readme | |
ob-mongosh
Org-mode Babel integration for running MongoDB scripts using the mongosh shell in Emacs.
Reworked version of krisajenkins/ob-mongo.
Example Usage
Simple
#+begin_src mongosh :uri "mongodb+srv://user:pass@host/db"
return db.collection.countDocuments({})
#+end_src
URI from shell
As the :url value get appended into the execution shell command, you can use
terminal commands as such pass to fill your URI:
#+begin_src mongosh :uri $(pass path/to/my/mongo/uri)
return db.collection.countDocuments({})
#+end_src
Advanced code block
It's Javascript, you can play around with it:
#+begin_src mongosh :uri "$(pass path/to/my/mongo/uri)" :wrap src json
const count = db.collection.countDocuments({})
const docName = db.collection.findOne({id: "1234" }).name
const time = new Date()
return JSON.stringify({
time,
docName,
count
})
#+end_src
#+RESULTS:
#+begin_src json{"time":"2025年10月08日T23:39:12.350Z","docName":"Document Name","count":10}
#+end_src
Table output
#+begin_src mongosh :uri "$(pass path/to/my/mongo/uri)" :results table
return JSON.stringify([
["name", "age"],
["John", "34"],
["Anne", "26"],
["Paul", "27"]
])
#+end_src
#+RESULTS:
| name | age |
| John | 34 |
| Anne | 26 |
| Paul | 27 |
Variable support
Variables can be injected into a code block using Babel's :var header:
#+begin_src mongosh :uri "mongodb://localhost" :var nums='(3 5 7) :var persons='((name . "Anne") (age . 22)) :var n=1
return nums.map(x => x+1)[0] + persons.age + n
#+end_src
#+RESULTS:
: 27
Limitations
As workaround to use a JS file instead injecting commands in the REPL, the
source block results requires a return statement.
Installation
Note that mongosh must be available in your PATH.
Doom Emacs
Add into your package.el:
(package! ob-mongosh
:recipe (:host codeberg :repo "thisago/ob-mongosh" :files ("*.el")))
Next steps
- Add unit tests for command builder