433dd886983da3b1eb4ca6040619a0fc0dec7128
[aai/esr-gui.git] /
1 [![Build Status](https://secure.travis-ci.org/christkv/mongodb-core.png)](http://travis-ci.org/christkv/mongodb-core)
2 [![Coverage Status](https://coveralls.io/repos/github/christkv/mongodb-core/badge.svg?branch=1.3)](https://coveralls.io/github/christkv/mongodb-core?branch=1.3)
3
4 # Description
5
6 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.
7
8 ## MongoDB Node.JS Core Driver
9
10 | what          | where                                          |
11 |---------------|------------------------------------------------|
12 | documentation | http://mongodb.github.io/node-mongodb-native/  |
13 | apidoc        | http://mongodb.github.io/node-mongodb-native/  |
14 | source        | https://github.com/christkv/mongodb-core       |
15 | mongodb       | http://www.mongodb.org/                        |
16
17 ### Blogs of Engineers involved in the driver
18 - Christian Kvalheim [@christkv](https://twitter.com/christkv) <http://christiankvalheim.com>
19
20 ### Bugs / Feature Requests
21
22 Think you’ve found a bug? Want to see a new feature in node-mongodb-native? Please open a
23 case in our issue management tool, JIRA:
24
25 - Create an account and login <https://jira.mongodb.org>.
26 - Navigate to the NODE project <https://jira.mongodb.org/browse/NODE>.
27 - Click **Create Issue** - Please provide as much information as possible about the issue type and how to reproduce it.
28
29 Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the
30 Core Server (i.e. SERVER) project are **public**.
31
32 ### Questions and Bug Reports
33
34  * mailing list: https://groups.google.com/forum/#!forum/node-mongodb-native
35  * jira: http://jira.mongodb.org/
36
37 ### Change Log
38
39 http://jira.mongodb.org/browse/NODE
40
41 # QuickStart
42
43 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.
44
45 ## Create the package.json file
46
47 Let's create a directory where our application will live. In our case we will put this under our projects directory.
48
49 ```
50 mkdir myproject
51 cd myproject
52 ```
53
54 Create a **package.json** using your favorite text editor and fill it in.
55
56 ```json
57 {
58   "name": "myproject",
59   "version": "1.0.0",
60   "description": "My first project",
61   "main": "index.js",
62   "repository": {
63     "type": "git",
64     "url": "git://github.com/christkv/myfirstproject.git"
65   },
66   "dependencies": {
67     "mongodb-core": "~1.0"
68   },
69   "author": "Christian Kvalheim",
70   "license": "Apache 2.0",
71   "bugs": {
72     "url": "https://github.com/christkv/myfirstproject/issues"
73   },
74   "homepage": "https://github.com/christkv/myfirstproject"
75 }
76 ```
77
78 Save the file and return to the shell or command prompt and use **NPM** to install all the dependencies.
79
80 ```
81 npm install
82 ```
83
84 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.
85
86 Booting up a MongoDB Server
87 ---------------------------
88 Let's boot up a MongoDB server instance. Download the right MongoDB version from [MongoDB](http://www.mongodb.org), 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**).
89
90 ```
91 mongod --dbpath=/data --port 27017
92 ```
93
94 You should see the **mongod** process start up and print some status information.
95
96 ## Connecting to MongoDB
97
98 Let's create a new **app.js** file that we will use to show the basic CRUD operations using the MongoDB driver.
99
100 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.
101
102 ```js
103 var Server = require('mongodb-core').Server
104   , assert = require('assert');
105
106 // Set up server connection
107 var server = new Server({
108     host: 'localhost'
109   , port: 27017
110   , reconnect: true
111   , reconnectInterval: 50
112 });
113
114 // Add event listeners
115 server.on('connect', function(_server) {
116   console.log('connected');
117   test.done();
118 });
119
120 server.on('close', function() {
121   console.log('closed');
122 });
123
124 server.on('reconnect', function() {
125   console.log('reconnect');
126 });
127
128 // Start connection
129 server.connect();
130 ```
131
132 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.
133
134 ```js
135 var Server = require('mongodb-core').Server
136   , assert = require('assert');
137
138 // Set up server connection
139 var server = new Server({
140     host: 'localhost'
141   , port: 27017
142   , reconnect: true
143   , reconnectInterval: 50
144 });
145
146 // Add event listeners
147 server.on('connect', function(_server) {
148   console.log('connected');
149
150   // Execute the ismaster command
151   _server.command('system.$cmd', {ismaster: true}, function(err, result) {
152
153     // Perform a document insert
154     _server.insert('myproject.inserts1', [{a:1}, {a:2}], {
155       writeConcern: {w:1}, ordered:true
156     }, function(err, results) {
157       assert.equal(null, err);
158       assert.equal(2, results.result.n);      
159
160       // Perform a document update
161       _server.update('myproject.inserts1', [{
162         q: {a: 1}, u: {'$set': {b:1}}
163       }], {
164         writeConcern: {w:1}, ordered:true
165       }, function(err, results) {
166         assert.equal(null, err);
167         assert.equal(1, results.result.n);
168
169         // Remove a document
170         _server.remove('myproject.inserts1', [{
171           q: {a: 1}, limit: 1
172         }], {
173           writeConcern: {w:1}, ordered:true
174         }, function(err, results) {
175           assert.equal(null, err);
176           assert.equal(1, results.result.n);
177
178           // Get a document
179           var cursor = _server.cursor('integration_tests.inserts_example4', {
180               find: 'integration_tests.example4'
181             , query: {a:1}
182           });
183
184           // Get the first document
185           cursor.next(function(err, doc) {
186             assert.equal(null, err);
187             assert.equal(2, doc.a);
188
189             // Execute the ismaster command
190             _server.command("system.$cmd"
191               , {ismaster: true}, function(err, result) {
192                 assert.equal(null, err)
193                 _server.destroy();              
194             });
195           });
196       });
197     });
198
199     test.done();
200   });
201 });
202
203 server.on('close', function() {
204   console.log('closed');
205 });
206
207 server.on('reconnect', function() {
208   console.log('reconnect');
209 });
210
211 // Start connection
212 server.connect();
213 ```
214
215 The core driver does not contain any helpers or abstractions only the core crud operations. These consist of the following commands.
216
217 * `insert`, Insert takes an array of 1 or more documents to be inserted against the topology and allows you to specify a write concern and if you wish to execute the inserts in order or out of order.
218 * `update`, Update takes an array of 1 or more update commands to be executed against the server topology and also allows you to specify a write concern and if you wish to execute the updates in order or out of order.
219 * `remove`, Remove takes an array of 1 or more remove commands to be executed against the server topology and also allows you to specify a write concern and if you wish to execute the removes in order or out of order.
220 * `cursor`, Returns you a cursor for either the 'virtual' `find` command, a command that returns a cursor id or a plain cursor id. Read the cursor tutorial for more inn depth coverage.
221 * `command`, Executes a command against MongoDB and returns the result.
222 * `auth`, Authenticates the current topology using a supported authentication scheme.
223
224 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.
225
226 ## Next steps
227
228 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.