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

mongode2

v1.2.1

Published

A CRUD package that is built on the mongoDB-node driver. Simplifies and streamlines the mongoDB-node.js stack development process.

Downloads

7

Readme

Mongode2 build status

Get your MongoDB--Node.js project off the ground faster.

This wrapper builds on top of the node-mongodb-native driver with prepackaged methods for quick and simple CRUD access to MongoDB from your Node.js project (along with some other toys).

Install

To install in your local project,

$ npm install mongode2 # Another unmaintained package uses the same name

Then in your project:

var db = new require('mongode2')('yourDbName', 'yourCollectionName');
// Use of the 'new' constructor allows you to maintain connections to multiple dbs/collections
var db2 = new require('mongode2')('anotherDbName', 'anotherCollectionName');

API

Class

db(opts)

The wrapper class. Should be invoked with new or as a constructor function:

var dbFactory = require('mongode2');
// using `new`
var db = new dbFactory({
    dbName: 'database name',
    colName: 'collection name'
});
// using as a constructor
var db2 = dbFactory('otherDb', 'otherCollection');
// both are equivalent.

Argument | Type | Description ------|------|------: dbName| String | Name of the database you want to connect to. This is appended to a MongoURI, which means **if you have to authenticate or for some reason. collectionName| String | Name of the collection you want to connect to. opts| Object | Optional An object of various options.

opts object:

Value | Type | Default | Description ------|------|:----:|-----: hostname|String|'localhost'|A valid hostname. port|String|'27017'|A valid port, preferably stored as a number, not a string.

db properties

db.count

Mongode keeps a cached count of how many entries are in the database stored in the .count property.

db methods

db.create(data [, callback]);

Insert data into the collection.

var Mongode = require('mongode2');
var db = new Mongode("db", "collection");

db.create({"foo": "bar", "testing": true}, function(err) {
    // Hooray!
    if (err) {
      console.log("Nice error handling!");
    }
});

Argument | Type | Description :----------|:---------|------------: data | Object | The document you want to insert. callback | Function | Optional Function to be executed upon completion.

callback() takes two parameters, err and res. If callback() is omitted, any error occurring will be thrown. It is highly recommended to use at least define callback(err) to handle the error and prevent crash-ridden, hard-to-debug database calls.

  • err -- The error object. If no error, this value will be null; otherwise object.
  • res -- The writeOpResult object. Contains useful diagnostic information like:
    • res.result.n: number of documents the operation actually inserted,

db.read([query,] callback)

Request all documents (no query) or all documents matching query.

// continuing from above:
db.read(/* Empty query means read all \*/, function(err, docs) {
    console.log(docs);
    // [{foo: "bar", testing: true}]
    // note it returned an array, even though there was only one entry.
});

Argument | Type | Description :----------|:---------|------------: query | Object(search Query) | An object containing a valid mongoDB query. callback | Function | The callback function containing the result of db.read.

If you're unsure about mongoDB's search queries, read up here.

callback takes two parameters,

  • err for the error object if there is one (otherwise null) and
  • docs, an array containing the array of docs from your query. Important note: even if the collection has only one entry, docs will be an array. Accessing properties of an array, e.g. docs.foo will return undefined. Access document properties inside the array, docs[0].foo. You have been warned.

db.update(query, data, [opts,] callback)

Update a document matching query with data.

db.update({"foo": {$exists: true}}, {$set: {"modified": "yep"}}, function(err, res) {
    /* Our doc is now  {
      foo: "bar",
      testing: true,
      modified: "yep"
    } \*/
    console.log(res.result.nModified);
    // 1
});

Argument | Type | Description :----------|:---------|------------: query|Object(search Query)|An object containing a valid mongoDB search query. data|Object(update Query)|An object containing a valid mongoDB update query. opts|Object| optional An object for multi and upsert options. callback|Function|The callback function with the error object if there was one.

If you're not sure about mongoDB's update queries, read up on them here.

If you want to update multiple docs or upsert on no matches, specify them in the options object.

var opts = {
  multi: true, // false by default
  upsert: true // false by default
};

callback takes two parameters

  • err: the error object (otherwise null) and
  • res: which is the result from MongoDB.

More documentation to come!