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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mungo

v2.3.2

Published

Mungo ===

Downloads

204

Readme

Mungo

Mungo is a library to create models for MongoDB.

Install

npm install mungo

Usage

import Mungo from 'mungo';

class User extends Mungo.Model {
  static schema = { username : String };
}

// Connect to MongoDB
Mungo.connect('mongodb://@localhost');

User.create({ username : 'dude' });

User.find({ username : 'dude' });

User.update({ username : 'dude' }, { username : 'mate' });

User.remove({ username : 'dude' });

Syntax

All operations are promises:

Foo.find()
  .then(found => { /*...*/ })
  .catch(error => { /*...*/ });

Some methods are chainable:

Foo
  .find({ foo : 1 })
  .limit(25)
  .then(found => {});

Connect

Mungo.connect(url);

Connecting by entering an url. Connect emits so you can listen to it:

Mungo.connect(url)
  .on('error', error => console.log(error.stack))
  .on('connected', connection => {
    console.log(connection.db)
  });

Connections are stacked in Mungo.connections. When you try to do a op, the first alive connection in array will be chosen.

You can force a connection to be used:

Mungo.connect(url1);
Mungo.connect(url2);

Foo.find().connection(0);
Foo2.find().connection(1);

Or specify the connection directly:

Mungo
  .connect(url)
  .on('connected', connection => Foo.find().connection(connection));

Methods

| Method | Arguments | About | |--------|-----------|-------| | count| - <object> query default {}- <object> projection default {} - <object> options default {}| Count documents in collection

Schema

Type

class User extends Mungo.Model {
  static schema = { username : String, score : Number };
}

Types allowed

  • String
  • Number
  • Boolean
  • Date
  • Array for a better array control, see below
  • Object for a better subdocument control, see below
  • Subdocument see below
  • ObjectID MongoDB's object id
  • Mixed accepts any type

Array of types

You can enclose types inside arrays:

// { numbers : [1, 2, 3] }

static schema = { numbers : [Number] }

Sudocuments

Use the Subdocument to embed a document:

// { foo : { bar : true } }

static schema = { "foo" : new (Mungo.Subdocument)({ "bar" : Boolean }) }

You could also use directly the object notation such as:

// { foo : { bar : true } }

static schema = { "foo" : { "bar" : Boolean } }

But you have to make sure your subdocument does not contain a type property - otherwise it will be mistaken with a field description.

References to other model

Use the name of the model you want to refer :

class Team extends Mungo.Model {
  static schema = { "name" : String };
}

class Player extends Mungo.Model {
  static schema = { "team" : Team };
}

Cyclic dependencies

If your model uses references to other models that also refer it (cyclic dependency), you can use the getter syntax to make sure referred models do not end up null.

static get schema () {
  return { "team" : Team };
}

Type declaration

You can use the sugar syntax or the type attribute in the field description:

static schema = { "name" : String }
// Or...
static schema = {
  name : { "type" : String }
}

Default type

If you don't declare a type for a field, Mixed is used.

Required

Require a fill to be set when inserting to DB

static schema = { "name" : { required : true } }

Default

Fill empty field values with default when inserting to DB

static schema = { "score" : { default : 0 } }