Skip to content

tanguylebarzic/node-mongodb-native

 
 

Repository files navigation

Install

Run:

make

Community

Check out the google group groups.google.com/group/node-mongodb-native for questions/answers from users of the driver.

Introduction

This is a node.js driver for MongoDB. It’s a port (or close to a port) of the libary for ruby at github.com/mongodb/mongo-ruby-driver/.

A simple example of inserting a document.

var client = new Db('integration_tests_20', new Server("127.0.0.1", 27017, {}));
client.open(function(err, p_client) {
	client.createCollection('test_insert', function(err, collection) {
	  client.collection('test_insert', function(err, collection) {
	    for(var i = 1; i < 1000; i++) {
	      collection.insert({c:1}, function(err, docs) {});
	    }

	    collection.insert({a:2}, function(err, docs) {
	      collection.insert({a:3}, function(err, docs) {
	        collection.count(function(err, count) {
	          test.assertEquals(1001, count);
	          // Locate all the entries using find
	          collection.find(function(err, cursor) {
	            cursor.toArray(function(err, results) {
	              test.assertEquals(1001, results.length);
	              test.assertTrue(results[0] != null);

	              // Let's close the db 
	              client.close();
	            });
	          });          
	        });        
	      });
	    });      
	  });    
	});
});

Important

The version V0.8.0 > contains a C/C++ native BSON parser, this leads to some small changes in the way you need to access the BSON classes as you need to use the right versions of the classes with the right driver.

For the existing javascript driver please reference BSON via

BSON = require(‘./mongodb’).BSONPure or when using the native bson parser use

BSON = require(‘./mongodb’).BSONNative To enable the driver to use the C/C++ bson parser pass it the option native_parser:true like below

var client = new Db(‘integration_tests_20’, new Server(“127.0.0.1”, 27017), {native_parser:true});

GitHub information

The source code is available at github.com/christkv/node-mongodb-native. You can either clone the repository or download a tarball of the latest release.

Once you have the source you can test the driver by running

$ make test

in the main directory. You will need to have a mongo instance running on localhost for the integration tests to pass.

Examples

For examples look in the examples/ directory. You can execute the examples using node.

$ cd examples $ node queries.js

GridStore

The GridStore class allows for storage of binary files in mongoDB using the mongoDB defined files and chunks collection definition.

See the gridfs.js file under examples/ for how to use it or view the integration tests marked with test_gs_…

Notes

The current version does not support connection pooling, but it will be implemented soon.

Primary Key Factories

Defining your own primary key factory allows you to generate your own series of id’s (this could f.ex be to use something like ISBN numbers). The generated the id needs to be a 12 byte long “string”.

Simple example below

// Custom factory (need to provide a 12 byte array); CustomPKFactory = function() {} CustomPKFactory.prototype = new Object(); CustomPKFactory.createPk = function() {

return new ObjectID(“aaaaaaaaaaaa”); }

var p_client = new Db(‘integration_tests_20’, new Server(“127.0.0.1”, 27017, {}), {‘pk’:CustomPKFactory}); p_client.open(function(err, p_client) { p_client.dropDatabase(function(err, done) {

p_client.createCollection(‘test_custom_key’, function(err, collection) { collection.insert({‘a’:1}, function(err, docs) { collection.find({‘_id’:new ObjectID(“aaaaaaaaaaaa”)}, function(err, cursor) { cursor.toArray(function(err, items) { test.assertEquals(1, items.length);

// Let's close the db

p_client.close(); }); }); }); }); }); });

Strict mode

Each database has an optional strict mode. If it is set then asking for a collection that does not exist will return an Error object in the callback. Similarly if you attempt to create a collection that already exists. Strict is provided for convenience.

var error_client = new Db(‘integration_tests_’, new Server(“127.0.0.1”, 27017, {auto_reconnect: false}), {strict:true}); test.assertEquals(true, error_client.strict); error_client.open(function(err, error_client) { error_client.collection(‘does-not-exist’, function(err, collection) { test.assertTrue(err instanceof Error); test.assertEquals(“Collection does-not-exist does not exist. Currently in strict mode.”, err.message);

});

error_client.createCollection(‘test_strict_access_collection’, function(err, collection) {

error_client.collection(‘test_strict_access_collection’, function(err, collection) { test.assertTrue(collection instanceof Collection); // Let’s close the db error_client.close(); }); }); });

Cursors

The cursors allow for the application to pull data lazily from mongoDB. The query is deferred until you request the first object using either .each/.nextObject/.toArray.

Documentation

Work in Progress

Sponsors

Just as Felix Geisendörfer I’m also working on the driver for my own startup and this driver is a big project that also benefits other companies who are using MongoDB.

If your company could benefit from a even better-engineered node.js mongodb driver I would appreciate any type of sponsorship you may be able to provide. All the sponsors will get a lifetime display in this readme, priority support and help on problems and votes on the roadmap decisions for the driver. If you are interested contact me on [christkv@gmail.com](christkv@gmail.com) for details.

And I’m very thankful for code contributions. If you are interested in working on features please contact me so we can discuss API design and testing.

Release Notes

See HISTORY

Credits

10gen: github.com/mongodb/mongo-ruby-driver/ Google: code.google.com/closure/library/ Jonas Raoni Soares Silva jsfromhell.com/classes/binary-parser

License

Copyright 2009 - 2010 Christian Amor Kvalheim.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

About

Mongo DB Native NodeJS Driver

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 77.1%
  • C++ 20.9%
  • C 2.0%