npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoson

v0.2.0

Published

Stringifies query objects for pasting into the Mongo shell.

Downloads

136

Readme

mongoson: MongoDB Shell Object Notation Build Status

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

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 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:

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

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

Which gives

{"_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.
Copyright (c) 2013 Meryn Stol