# mongoson

> Stringifies query objects for pasting into the Mongo shell.

Latest version **0.2.0** (published 2015-04-14) · 0 weekly downloads

## Install

```sh
npm install mongoson
pnpm add mongoson
yarn add mongoson
bun add mongoson
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2015-04-14 |
| First published | 2013-02-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10 |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Meryn Stol |
| Maintainers | meryn |
| Keywords | mongodb, json, serializer, stringify, ObjectId, ISODate, DBRef |

## Links

- npm: https://www.npmjs.com/package/mongoson
- Repository: https://github.com/meryn/mongoson
- Issues: https://github.com/meryn/mongoson/issues
- npm.io page: https://npm.io/package/mongoson

## Dependencies (1)

- [bson](https://npm.io/package/bson.md) 0.3.x

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.2.0 (latest) — 2015-04-14
- 0.1.3 — 2013-06-10
- 0.1.1 — 2013-04-13
- 0.1.0 — 2013-04-13
- 0.0.4 — 2013-02-07
- 0.0.3 — 2013-02-07
- 0.0.2 — 2013-02-07
- 0.0.1 — 2013-02-07

## README

# mongoson: MongoDB Shell Object Notation [![Build Status](https://travis-ci.org/meryn/mongoson.png?branch=master)](https://travis-ci.org/meryn/mongoson)

`MSON.stringify` is an alternative for `JSON.stringify`. It serializes queries (and data to be inserted or updated) in such a way that they can be pasted into the Mongo shell, with no loss of information. ObjectIds, DBRefs and Dates are not converted to strings, so they'll keep working. 

The primary use case is to help with debugging MongoDB queries that are generated by your app.

## Installation

```
npm install mongoson
```

## Usage

### MSON.stringify

```coffee
MSON = require 'mongoson'
MSON.stringify mongoQuery
```

This returns a serialized query, a "query object literal" if you will. The result can be pasted into the mongo console.

#### Notes

* `MSON.stringify` encodes ObjectId, DBRef and Date objects. All else should be equivalent to regular JSON. 
* Passing a replacer function as second argument is not supported.
* Indentention is not supported.

### MSON.parseUnsafe

For the brave-at-heart, there's also `MSON.parseUnsafe serializedQuery`. It's called `parseUnsafe` for a reason, because it actually uses `eval` to instantiate the correct `ObjectId`, `DBRef` and `Date` objects, *without doing any sanitation beforehand*. You do NOT want to use this unattended. Before running it on any serialized query, I recommend scrutinizing it for any fishy stuff inside. _It could absolutely execute any kind of code in node.js._

The advantage of `MSON.parseUnsafe` over doing eval yourself is that the code gets evaluated in a context where `ObjectId`, `DBRef`, and `ISODate` functions are defined. This is quicker than importing them from MongoDB's BSON module yourself.

I'd appreciate any hints on how to adjust (for example) the [JSON2](https://github.com/douglascrockford/JSON-js) parse function to let the specific calls to `ObjectId`, `DBRef` and `ISODate` pass through, while disallowing any other kinds of expressions (beyond valid JSON expressions, obviously). Then we could have a safe `MSON.parse`.

## Example

Suppose you have build a query using some "native" Mongo object types (DBRef and ObjectId), like so:

```coffee
bson = require 'bson'
ObjectID = bson.BSONPure.ObjectID
DBRef = bson.BSONPure.DBRef

someQuery = 
  _id: ObjectID("507f1f77bcf86cd799439011")
  title: "Super"
  related: [
    ObjectID("507f1f77bcf86cd799439011"), 
    ObjectID("507f1f77bcf86cd799439012"), 
    ObjectID("507f1f77bcf86cd799439013")
  ]
  owner: DBRef("groups",ObjectID("507f191e810c19729de860ea"))
  updatedAt: 
    $gte: new Date "2012-02-07T18:32:42.692Z" 
    $lte: new Date "2013-02-07T18:32:42.692Z"
```

You can then do

```coffee
MSON = require 'mongoson'
console.log MSON.stringify someQuery
```

Which gives

```javascript
{"_id":ObjectId("507f1f77bcf86cd799439011"),"title":"Super","related":[ObjectId("507f1f77bcf86cd799439011"),ObjectId("507f1f77bcf86cd799439012"),ObjectId("507f1f77bcf86cd799439013")],"owner":{"$ref":"groups","$id":"507f191e810c19729de860ea"},"updatedAt":{"$gte":ISODate("2012-02-07T18:32:42.692Z"),"$lte":ISODate("2013-02-07T18:32:42.692Z")}}
```

This is ready to be pasted into the MongoDB shell as part of a command.

The example input and output is taken straight from the (sole) test for this module, so the above should absolutely work.

## License

mongoson is released under the [MIT License](http://opensource.org/licenses/MIT).  
Copyright (c) 2013 Meryn Stol

---
_Source: https://npm.io/package/mongoson · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
