1.2.1 • Published 9 years ago

mongode2 v1.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

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.
ArgumentTypeDescription
dbNameStringName 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.
collectionNameStringName of the collection you want to connect to.
optsObjectOptional An object of various options.

opts object:

ValueTypeDefaultDescription
hostnameString'localhost'A valid hostname.
portString'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!");
    }
});
ArgumentTypeDescription
dataObjectThe document you want to insert.
callbackFunctionOptional 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.
});
ArgumentTypeDescription
queryObject(search Query)An object containing a valid mongoDB query.
callbackFunctionThe 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
});
ArgumentTypeDescription
queryObject(search Query)An object containing a valid mongoDB search query.
dataObject(update Query)An object containing a valid mongoDB update query.
optsObjectoptional An object for multi and upsert options.
callbackFunctionThe 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!