meteor/mongodb-core

Name: mongodb-core

Owner: Meteor

Description: MongoDB core driver functionality aims to make the "smallest" viable driver api

Forked from: mongodb-js/mongodb-core

Created: 2017-10-05 01:26:55.0

Updated: 2017-10-05 01:26:57.0

Pushed: 2017-10-09 23:32:38.0

Homepage: null

Size: 4522

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status Coverage Status

Description

The MongoDB Core driver is the low level part of the 2.0 or higher MongoDB driver and is meant for library developers not end users. It does not contain any abstractions or helpers outside of the basic management of MongoDB topology connections, CRUD operations and authentication.

MongoDB Node.JS Core Driver

| what | where | |—————|————————————————| | documentation | http://mongodb.github.io/node-mongodb-native/ | | apidoc | http://mongodb.github.io/node-mongodb-native/ | | source | https://github.com/christkv/mongodb-core | | mongodb | http://www.mongodb.org/ |

Blogs of Engineers involved in the driver
Bugs / Feature Requests

Think you?ve found a bug? Want to see a new feature in node-mongodb-native? Please open a case in our issue management tool, JIRA:

Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are public.

Questions and Bug Reports
Change Log

http://jira.mongodb.org/browse/NODE

QuickStart

The quick start guide will show you how to set up a simple application using Core driver and MongoDB. It scope is only how to set up the driver and perform the simple crud operations. For more inn depth coverage we encourage reading the tutorials.

Create the package.json file

Let's create a directory where our application will live. In our case we will put this under our projects directory.

r myproject
yproject

Create a package.json using your favorite text editor and fill it in.


ame": "myproject",
ersion": "1.0.0",
escription": "My first project",
ain": "index.js",
epository": {
"type": "git",
"url": "git://github.com/christkv/myfirstproject.git"

ependencies": {
"mongodb-core": "~1.0"

uthor": "Christian Kvalheim",
icense": "Apache 2.0",
ugs": {
"url": "https://github.com/christkv/myfirstproject/issues"

omepage": "https://github.com/christkv/myfirstproject"

Save the file and return to the shell or command prompt and use NPM to install all the dependencies.

install

You should see NPM download a lot of files. Once it's done you'll find all the downloaded packages under the node_modules directory.

Booting up a MongoDB Server

Let's boot up a MongoDB server instance. Download the right MongoDB version from MongoDB, open a new shell or command line and ensure the mongod command is in the shell or command line path. Now let's create a database directory (in our case under /data).

od --dbpath=/data --port 27017

You should see the mongod process start up and print some status information.

Connecting to MongoDB

Let's create a new app.js file that we will use to show the basic CRUD operations using the MongoDB driver.

First let's add code to connect to the server. Notice that there is no concept of a database here and we use the topology directly to perform the connection.

Server = require('mongodb-core').Server
assert = require('assert');

et up server connection
server = new Server({
host: 'localhost'
port: 27017
reconnect: true
reconnectInterval: 50


dd event listeners
er.on('connect', function(_server) {
nsole.log('connected');
st.done();


er.on('close', function() {
nsole.log('closed');


er.on('reconnect', function() {
nsole.log('reconnect');


tart connection
er.connect();

To connect to a replicaset we would use the ReplSet class and for a set of Mongos proxies we use the Mongos class. Each topology class offer the same CRUD operations and you operate on the topology directly. Let's look at an example exercising all the different available CRUD operations.

Server = require('mongodb-core').Server
assert = require('assert');

et up server connection
server = new Server({
host: 'localhost'
port: 27017
reconnect: true
reconnectInterval: 50


dd event listeners
er.on('connect', function(_server) {
nsole.log('connected');

 Execute the ismaster command
erver.command('system.$cmd', {ismaster: true}, function(err, result) {

// Perform a document insert
_server.insert('myproject.inserts1', [{a:1}, {a:2}], {
  writeConcern: {w:1}, ordered:true
}, function(err, results) {
  assert.equal(null, err);
  assert.equal(2, results.result.n);      

  // Perform a document update
  _server.update('myproject.inserts1', [{
    q: {a: 1}, u: {'$set': {b:1}}
  }], {
    writeConcern: {w:1}, ordered:true
  }, function(err, results) {
    assert.equal(null, err);
    assert.equal(1, results.result.n);

    // Remove a document
    _server.remove('myproject.inserts1', [{
      q: {a: 1}, limit: 1
    }], {
      writeConcern: {w:1}, ordered:true
    }, function(err, results) {
      assert.equal(null, err);
      assert.equal(1, results.result.n);

      // Get a document
      var cursor = _server.cursor('integration_tests.inserts_example4', {
          find: 'integration_tests.example4'
        , query: {a:1}
      });

      // Get the first document
      cursor.next(function(err, doc) {
        assert.equal(null, err);
        assert.equal(2, doc.a);

        // Execute the ismaster command
        _server.command("system.$cmd"
          , {ismaster: true}, function(err, result) {
            assert.equal(null, err)
            _server.destroy();              
        });
      });
  });
});

test.done();
;


er.on('close', function() {
nsole.log('closed');


er.on('reconnect', function() {
nsole.log('reconnect');


tart connection
er.connect();

The core driver does not contain any helpers or abstractions only the core crud operations. These consist of the following commands.

The Core Driver is a building block for library builders and is not meant for usage by end users as it lacks a lot of features the end user might need such as automatic buffering of operations when a primary is changing in a replicaset or the db and collections abstraction.

Next steps

The next step is to get more in depth information about how the different aspects of the core driver works and how to leverage them to extend the functionality of the cursors. Please view the tutorials for more detailed information.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.