Coder Social home page Coder Social logo

arronzhang / mongoq Goto Github PK

View Code? Open in Web Editor NEW
68.0 5.0 5.0 433 KB

Use mongoDB like this: mongoq('mongodb://localhost/db').collection('users').find().toArray(function(error, docs){});

Home Page: http://zzdhidden.github.com/mongoq

Makefile 0.80% JavaScript 99.20%

mongoq's Introduction

MongoQ

Use mongoDB like this:

mongoq("testdb").collection("users").find().toArray().done( function(docs){} ).fail( function(err){} );

Base on node-mongodb-native

Features

Installation

npm install mongoq

Example

var assert = require("assert")
	, mongoq = require("../index.js")
	, db = mongoq("mongodb://127.0.0.1:27017/mongoqTest")
	, User = db.collection("users");

User.remove() //clear test date
	.done( function() {
		User.insert( [{ _id: 1, name: "Jack", age: 22 }, { _id: 2, name: "Lucy", age: 20 }] ) //Add Jack and Lucy
			.and( User.insert( { _id: 3, name: "Mike", age: 21 } ) ) //Add Mike synchronous
			.and( function(u1, u2) {
				// Will find after add Jack, Lucy and Mike
				return User.findOne( { name: u2[0]["name"] } )
			} )
			.done( function(u1, u2, u3) { //All be done
				assert.deepEqual( u1, [{ _id: 1, name: "Jack", age: 22 }, { _id: 2, name: "Lucy", age: 20 }], "Insert first" );
				assert.deepEqual( u2, [{ _id: 3, name: "Mike", age: 21 }], "insert second" );
				assert.deepEqual( u3, { _id: 3, name: "Mike", age: 21 }, "Find after insert" );
				db.close();
			} )
			.fail( function( err ) { // Any error occur
				console.log( err );
			} );
	} )
	.fail( function( err ) { // Faild to remove
		console.log( err );
	} );

Work like node-mongodb-native

Mongoq bridge all the methods and events from mongodb native database and mongodb native collections, and make it chainable.

###Access BSON

var mongoq = require("mongoq");

var BSON = mongoq.BSON;
var ObjectID = BSON.ObjectID;

###Database

Provide a simple [connection string][connection string]

var mongoq = require("mongoq");

//use default server localhost:27017, poolSize 1
var db = mongoq("testdb"); 

//use options
db = mongoq("testdb", {host: "127.0.0.1", port: "27017"}); 

//connection string
db = mongoq("mongodb://localhost/testdb"); 

// Connect and login to the "testdb" database as user "admin" with passowrd "foobar"
db = mongoq("mongodb://admin:foobar@localhost:27017/testdb?poolSize=2");

//Repl set servers
db = mongoq("mongodb://admin:foobar@localhost:27017,localhost:27018/testdb?reconnectWait=2000;retries=20");

//Add user
db.addUser("admin", "foobar", function(err) {});

methods

  • close(callback)
  • admin(callback)
  • collectionNames(collectionName?, callback)
  • collection(collectionName, options?, callback)
  • collections(callback)
  • dereference(dbRef, callback)
  • logout(options, callback) Logout user from server, Fire off on all connections and remove all auth info
  • authenticate(username, password, callback)
  • addUser(username, password, callback)
  • removeUser(username, callback)
  • createCollection(collectionName, options?, callback)
  • dropCollection(collectionName, callback)
  • renameCollection(fromCollection, toCollection, callback)
  • lastError(options, connectionOptions, callback)
  • error(options, callback)
  • lastStatus(callback)
  • previousErrors(callback)
  • executeDbCommand(commandHash, options?, callback)
  • executeDbAdminCommand(commandHash, callback)
  • resetErrorHistory(callback)
  • createIndex(collectionName, fieldOrSpec, options?, callback) Create an index on a collection
  • ensureIndex(collectionName, fieldOrSpec, options?, callback) Ensure index, create an index if it does not exist
  • dropIndex(collectionName, indexName, callback) Drop Index on a collection
  • indexInformation(collectionName, options..., callback)
  • dropDatabase(callback)
  • cursorInfo(callback) Fetch the cursor information
  • executeCommand(dbCommand, options, callback)

###Collection

var mongoq = require("mongoq");
var db = mongoq("mongodb://localhost/testdb"); 
var users = db.collection("users");
users.insert({name: "Jack", phone: 1234567, email: "[email protected]"});

methods

  • insert (docs, options?, callback?)
  • remove (selector?, options?, callback?)
  • rename (newName, callback)
  • insertAll (docs, options?, callback?)
  • save (doc, options?, callback?)
  • update (selector, document, options?, callback?) // options:upsert,multi,safe
  • distinct (key, query?, callback?)
  • count (query?, callback)
  • drop (callback)
  • findAndModify (query, sort, doc, options?, callback?) // options: remove,unshift,new
  • find () //return Cursor
  • findOne (queryObject, options?, callback)
  • createIndex (fieldOrSpec, options, callback?)
  • ensureIndex (fieldOrSpec, options, callback?)
  • indexInformation (options, callback)
  • dropIndex (name, callback)
  • dropIndexes (callback)
  • mapReduce (map, reduce, options, callback)
  • group (keys, condition, initial, reduce, command, callback)
  • options (callback)

###Cursor

var mongoq = require("mongoq");
var db = mongoq("mongodb://localhost/testdb"); 
var users = db.collection("users");
var cursor = users.find();
cursor.toArray(function(err, users){
	db.close();
});

methods

  • toArray(callback)
  • each(callback)
  • count(callback)
  • sort(keyOrList, direction) //=> this
  • limit(limit) //=> this
  • skip(limit) //=> this
  • batchSize(limit) //=> this
  • nextObject(callback)
  • getMore(callback)
  • explain(callback)

MongoQ style

###Deferred Object

MongoQ introduce into jQuery Deferred since v0.2, you can find more documents about jQuery Deferred Object at here.

MongoQ make all mongodb asynchronous processes to return with a Promise Object.

var mongoq = require("mongoq");
var db = mongoq("mongodb://localhost/testdb"); 
var users = db.collection("users");
users.find().toArray()
    .done( function( docs ) { 
        //=> users
    } )
    .done( function( docs ) { 
        //=> users
    } )
    .fail( function( error ) { 
        //=> error
    } )
    .then( function( docs ) { 
        //=> users
    }, function( error ) { 
        //=> error
    } );

methods

  • done( doneCallbacks [, doneCallbacks] ) //=> Add handlers to be called when the Deferred object is resolved.
  • fail( failCallbacks [, failCallbacks] ) //=> Add handlers to be called when the Deferred object is rejected.
  • then( doneCallbacks, failCallbacks ) //=> Add handlers to be called when the Deferred object is resolved or rejected.
  • always( alwaysCallbacks ) //=> Add handlers to be called when the Deferred object is either resolved or rejected.

Notice: Please don't use find().each().done(..., the callbacks will be called only once.

###Control-flow

MongoQ add two methods called and and next to the Promise Object for the mongodb's parallel execution, serial execution and error handling painless.

and: run promise object series or parallel and then serialize the result

var mongoq = require("mongoq");
var db = mongoq("mongodb://localhost/testdb"); 
var users = db.collection("users");
var messages = db.collection("messages");
users.count()
    .and( users.findOne() ) // parallel
    .and( function( user ) { // serial when in function
        return user ? messages.find({ user: user._id }).toArray() : [];
    } )
    .done( function( num, user, msgs ) {
        //num from users.count
        //user from users.findOne
        //msgs from messages.find
    } )
    .fail( function( err ) {} );

next: run promise object series then give the result to the next

var mongoq = require("mongoq");
var db = mongoq("mongodb://localhost/testdb"); 
var users = db.collection("users");
var messages = db.collection("messages");
users.findOne()
    .next( function( user ) { // serial when in function
        return user ? messages.find({ user: user._id }).toArray() : [];
    } )
    .done( function( msgs ) {
        //msgs from messages.find
    } )
    .fail( function( err ) {} );

Contributor

License

(The MIT License)

Copyright (c) 2011 hidden <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

mongoq's People

Contributors

arronzhang avatar bydga avatar wision avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mongoq's Issues

Multiple connections?

How to manage multiple connections? I don't get it. What is the pool size? I want to use the same connection for all databases, how can I select a database without reconnecting?

var db = mongoq('mongodb://localhost/db1') // new connection
var db2 = mongoq('db2') // new connection or the same as above?

Thanks.

find by _id

I'm trying to use mongoq to find an item by _id which is currently a string - how do I get it to be an object ID?

var db = require("mongoq");
var coltest = db('mongodb://127.0.0.1/coltest');
var Posts = forumplus.collection("Posts");

Posts.findOne({_id:'4dd3a7d8a48efdcf19000001'},function exfindPostById(err,doc){
//Here is my info :)
});

read preferences with replicaSets

hi folks,

Can anyone help me with: howto set the readPreferences when using replicaSets with mongoq.

Settings e.g: mongoq("mongodb://localhost:27017,localhost:27018,localhost:27019/mydb")

So i need to have the Read's to one of the secondaries.

thx in advance,
mcpomm

Multiple connections?

How to manage multiple connections? I don't get it. What is the pool size? I want to use the same connection for all databases, how can I do that?

var db = mongoq('mongodb://localhost/db1') // new connection
var db2 = mongoq('db2') // new connection or the same as above?

Thanks.

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.