Giter Site home page Giter Site logo

mongo-col's Introduction

mongo-col Build Status

mongoDB collection wrapper

Status: production ready

Example

With mongo-db native

var Db = require('mongodb').Db,
    Server = require('mongodb').Server

var db = new Db('integration_tests',
    new Server("127.0.0.1", 27017, {
       auto_reconnect: false,
       poolSize: 4
    }))

db.open(function(err, db) {
    db.collection("CollectionName", function(err, collection) {
        collection.insert({hello:'world_no_safe'}, function () {
            collection.findOne({hello:'world_no_safe'}, function(err, item) {
                assert.equal(null, err)
                assert.equal('world_no_safe', item.hello)
                db.close()
            })
        })
    })
})

With mongo-col

var collection = require("mongo-col"),
    CollectionName = collection("CollectionName", "integration_tests")

CollectionName.insert({ hello: "world_no_safe"}, function () {
    CollectionName.findOne({ hello: "world_no_safe"}, function (err, item) {
        assert.equal(null, err)
        assert.equal('world_no_safe', item.hello)
        CollectionName.close()
    })
})

Motivation

Setting up a mongodb database connection requires too much callback soup, remove it.

Documentation

collection(collectionName[, databaseName])

collection takes a collection name and returns a collection object. This collection object has all the mongodb collection methods and sets up a database connection internally

See the MongoDB collection API

var collection = require("mongo-col"),
    Users = collection("Users", "optionalDatabaseName")

Users.insert({
    name: "foo",
    password: "bar"
})

You can optionally pass in a databaseName as a string or an instance of a mongodb database object.

Benchmarks

$ npm run-script benchmark

mongoskin benchmark took  11501 66
global native benchmark took  7790 49
mongoose benchmark took  28422 218
collection benchmark took  7947 53

Installation

npm install mongo-col

Tests

npm test

Contributors

  • Raynos

MIT Licenced

mongo-col's People

Contributors

nickjj avatar raynos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mongo-col's Issues

How to use MongoDB's ObjectID

Hi!

I'm trying to do this kind of thing (that I used to do with MongoJs)

this.update({ _id: this.ObjectId(project._id) }, {
  $set: { code: project_code, name: project.name }
});

Is there any equivalent with mongo-col?

Thanks!

MongoClient is pretty cool

function mongo(collectionName, databaseName) {
    var cachedCollection
    var queue

    return getCollection

    function getCollection(callback) {
        if (cachedCollection) {
            return callback(null, cachedCollection)
        } else if (queue) {
            return queue.push(callback)
        }

        queue = [callback]

        console.log("uri", config)

        MongoClient.connect(config.DB_URL, {}, function (err, db) {
            if (err) {
                throw err
            }

            var col = db.collection(collectionName)

            queue.forEach(function (f) { f(null, col) })

            console.log("db.db?", db.db)
        })
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.