Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.3KB

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