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

moko-mongo

v0.1.0

Published

mongo adapter for moko

Downloads

4

Readme

moko-mongo

mongo adapter for moko.

Installation

npm install moko-mongo

Usage Example

var mongo = require('moko-mongo');

db = yield mongo('mongodb://localhost:27017/test') // opens a connection
User.use(db);

var bob = yield new User({name: 'Bob'});
yield bob.save();
console.log(bob._id);

Configuration

moko-mongo will use Model.modelName for the collection. For example:

var mongo = yield require('moko-mongo')(connectString);
var User = moko('User').attr('_id').attr('name');
User.use(mongo) // uses 'User' for collection

If you would like to specify a different collection name, you can do so by passing it as an argument to the plugin.

User.use(mongo('People'));

Full Documentation

Model.db

Raw Kongo collection that you can work with directly.

Model.all(query, [options])

Queries for all models in the collection given the query. Additional options can be passed in.

Returns an array of instances that match, or an empty array if nothing matched.

var confirmedUsers = yield User.all({confirmed: true}, { sort: {confirmedAt: -1}, limit: 10});

Model.find/get(query, [options])

Queries for a specific model in the collection. Returns the first instance to match the query. Additional options can be passed in.

Also supports taking a String for the query for the _id.

If no documents match, returns false.

var user = yield User.get('536b8b39e7449b020000000b'); // Look up via string
    user = yield User.get({_id: '536b8b39e7449b020000000b'}); // Look up via string
    user = yield User.get({age: 30}, {sort: {createdAt: -1}}); // Look up via string

Model.removeAll(query, [options])

Removes all documents that match the query. Returns the number of documents removed

var removed = yield User.removeAll({deleted: true});
console.log("%d user accounts were removed", removed);

Model.index(fields, [options])

Alias to Model.db.ensureIndex. Lets you make indexes!

Model.query()

Returns a wrapped instances of mquery. See mquery support below.

Model.aggregate(bool)

Returns a wrapped instances of maggregate. See maggregate support below.

Optionally, if don't want it to return Model instances, you can use Model.aggregate(true) to skip wrapping the instances.

mquery support

moko-mongo provides a wrapped version of the wonderful mquery query builder. To get it, simply call Model.query(). This allows you to build readable and robust queries easily. When approprirate, modella-mongo will return instances of modella models, instead of just documents. Aside from that, it follows the mquery API completely.

Example with mquery

  var bob = yield User.query().findOne().where({username: 'Bob'})

maggregate support

moko-mongo uses the maggregate aggregation builder. To use it, simply call Model.aggregate().

This allows you to build readable aggregations easily. By default it wraps responses in Model instances, but can be disabled by passing skipWrap as true. It also follows the maggregate api completely.

Example with maggregate

var skipWrapping = true;
locations = yield User.aggregate(skipWrapping)
  .group({_id: '$location', userCount: {$sum: 1}});

locations.forEach(function(location) {
  console.log("%s has %d users", location._id, userCount);
});