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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. [![npm](https://nodei.co/npm/mongodb.png?downloads=true&downloadRank=true)](https://nodei.co/npm/mongodb/) [![npm](https://nodei.co/npm-dl/mongodb.png?months=6&height=3)](https://nodei.co/npm/mongodb/)
  2. [![Build Status](https://secure.travis-ci.org/mongodb/node-mongodb-native.svg?branch=2.1)](http://travis-ci.org/mongodb/node-mongodb-native)
  3. [![Coverage Status](https://coveralls.io/repos/github/mongodb/node-mongodb-native/badge.svg?branch=2.1)](https://coveralls.io/github/mongodb/node-mongodb-native?branch=2.1)
  4. [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/mongodb/node-mongodb-native?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
  5. # Description
  6. The official [MongoDB](https://www.mongodb.com/) driver for Node.js. Provides a high-level API on top of [mongodb-core](https://www.npmjs.com/package/mongodb-core) that is meant for end users.
  7. **NOTE: v3.x was recently released with breaking API changes. You can find a list of changes [here](CHANGES_3.0.0.md).**
  8. ## MongoDB Node.JS Driver
  9. | what | where |
  10. |---------------|------------------------------------------------|
  11. | documentation | http://mongodb.github.io/node-mongodb-native |
  12. | api-doc | http://mongodb.github.io/node-mongodb-native/3.1/api |
  13. | source | https://github.com/mongodb/node-mongodb-native |
  14. | mongodb | http://www.mongodb.org |
  15. ### Bugs / Feature Requests
  16. Think you’ve found a bug? Want to see a new feature in `node-mongodb-native`? Please open a
  17. case in our issue management tool, JIRA:
  18. - Create an account and login [jira.mongodb.org](https://jira.mongodb.org).
  19. - Navigate to the NODE project [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE).
  20. - Click **Create Issue** - Please provide as much information as possible about the issue type and how to reproduce it.
  21. Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the
  22. Core Server (i.e. SERVER) project are **public**.
  23. ### Questions and Bug Reports
  24. * Mailing List: [groups.google.com/forum/#!forum/node-mongodb-native](https://groups.google.com/forum/#!forum/node-mongodb-native)
  25. * JIRA: [jira.mongodb.org](http://jira.mongodb.org)
  26. ### Change Log
  27. Change history can be found in [`HISTORY.md`](HISTORY.md).
  28. # Installation
  29. The recommended way to get started using the Node.js 3.0 driver is by using the `npm` (Node Package Manager) to install the dependency in your project.
  30. ## MongoDB Driver
  31. Given that you have created your own project using `npm init` we install the MongoDB driver and its dependencies by executing the following `npm` command.
  32. ```bash
  33. npm install mongodb --save
  34. ```
  35. This will download the MongoDB driver and add a dependency entry in your `package.json` file.
  36. You can also use the [Yarn](https://yarnpkg.com/en) package manager.
  37. ## Troubleshooting
  38. The MongoDB driver depends on several other packages. These are:
  39. * [mongodb-core](https://github.com/mongodb-js/mongodb-core)
  40. * [bson](https://github.com/mongodb/js-bson)
  41. * [kerberos](https://github.com/mongodb-js/kerberos)
  42. * [node-gyp](https://github.com/nodejs/node-gyp)
  43. The `kerberos` package is a C++ extension that requires a build environment to be installed on your system. You must be able to build Node.js itself in order to compile and install the `kerberos` module. Furthermore, the `kerberos` module requires the MIT Kerberos package to correctly compile on UNIX operating systems. Consult your UNIX operation system package manager for what libraries to install.
  44. **Windows already contains the SSPI API used for Kerberos authentication. However, you will need to install a full compiler tool chain using Visual Studio C++ to correctly install the Kerberos extension.**
  45. ### Diagnosing on UNIX
  46. If you don’t have the build-essentials, this module won’t build. In the case of Linux, you will need gcc, g++, Node.js with all the headers and Python. The easiest way to figure out what’s missing is by trying to build the Kerberos project. You can do this by performing the following steps.
  47. ```bash
  48. git clone https://github.com/mongodb-js/kerberos
  49. cd kerberos
  50. npm install
  51. ```
  52. If all the steps complete, you have the right toolchain installed. If you get the error "node-gyp not found," you need to install `node-gyp` globally:
  53. ```bash
  54. npm install -g node-gyp
  55. ```
  56. If it correctly compiles and runs the tests you are golden. We can now try to install the `mongod` driver by performing the following command.
  57. ```bash
  58. cd yourproject
  59. npm install mongodb --save
  60. ```
  61. If it still fails the next step is to examine the npm log. Rerun the command but in this case in verbose mode.
  62. ```bash
  63. npm --loglevel verbose install mongodb
  64. ```
  65. This will print out all the steps npm is performing while trying to install the module.
  66. ### Diagnosing on Windows
  67. A compiler tool chain known to work for compiling `kerberos` on Windows is the following.
  68. * Visual Studio C++ 2010 (do not use higher versions)
  69. * Windows 7 64bit SDK
  70. * Python 2.7 or higher
  71. Open the Visual Studio command prompt. Ensure `node.exe` is in your path and install `node-gyp`.
  72. ```bash
  73. npm install -g node-gyp
  74. ```
  75. Next, you will have to build the project manually to test it. Clone the repo, install dependencies and rebuild:
  76. ```bash
  77. git clone https://github.com/christkv/kerberos.git
  78. cd kerberos
  79. npm install
  80. node-gyp rebuild
  81. ```
  82. This should rebuild the driver successfully if you have everything set up correctly.
  83. ### Other possible issues
  84. Your Python installation might be hosed making gyp break. Test your deployment environment first by trying to build Node.js itself on the server in question, as this should unearth any issues with broken packages (and there are a lot of broken packages out there).
  85. Another tip is to ensure your user has write permission to wherever the Node.js modules are being installed.
  86. ## Quick Start
  87. This guide will show you how to set up a simple application using Node.js and MongoDB. Its scope is only how to set up the driver and perform the simple CRUD operations. For more in-depth coverage, see the [tutorials](docs/reference/content/tutorials/main.md).
  88. ### Create the `package.json` file
  89. First, create a directory where your application will live.
  90. ```bash
  91. mkdir myproject
  92. cd myproject
  93. ```
  94. Enter the following command and answer the questions to create the initial structure for your new project:
  95. ```bash
  96. npm init
  97. ```
  98. Next, install the driver dependency.
  99. ```bash
  100. npm install mongodb --save
  101. ```
  102. 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.
  103. ### Start a MongoDB Server
  104. For complete MongoDB installation instructions, see [the manual](https://docs.mongodb.org/manual/installation/).
  105. 1. Download the right MongoDB version from [MongoDB](https://www.mongodb.org/downloads)
  106. 2. Create a database directory (in this case under **/data**).
  107. 3. Install and start a ``mongod`` process.
  108. ```bash
  109. mongod --dbpath=/data
  110. ```
  111. You should see the **mongod** process start up and print some status information.
  112. ### Connect to MongoDB
  113. Create a new **app.js** file and add the following code to try out some basic CRUD
  114. operations using the MongoDB driver.
  115. Add code to connect to the server and the database **myproject**:
  116. ```js
  117. const MongoClient = require('mongodb').MongoClient;
  118. const assert = require('assert');
  119. // Connection URL
  120. const url = 'mongodb://localhost:27017';
  121. // Database Name
  122. const dbName = 'myproject';
  123. // Use connect method to connect to the server
  124. MongoClient.connect(url, function(err, client) {
  125. assert.equal(null, err);
  126. console.log("Connected successfully to server");
  127. const db = client.db(dbName);
  128. client.close();
  129. });
  130. ```
  131. Run your app from the command line with:
  132. ```bash
  133. node app.js
  134. ```
  135. The application should print **Connected successfully to server** to the console.
  136. ### Insert a Document
  137. Add to **app.js** the following function which uses the **insertMany**
  138. method to add three documents to the **documents** collection.
  139. ```js
  140. const insertDocuments = function(db, callback) {
  141. // Get the documents collection
  142. const collection = db.collection('documents');
  143. // Insert some documents
  144. collection.insertMany([
  145. {a : 1}, {a : 2}, {a : 3}
  146. ], function(err, result) {
  147. assert.equal(err, null);
  148. assert.equal(3, result.result.n);
  149. assert.equal(3, result.ops.length);
  150. console.log("Inserted 3 documents into the collection");
  151. callback(result);
  152. });
  153. }
  154. ```
  155. The **insert** command returns an object with the following fields:
  156. * **result** Contains the result document from MongoDB
  157. * **ops** Contains the documents inserted with added **_id** fields
  158. * **connection** Contains the connection used to perform the insert
  159. Add the following code to call the **insertDocuments** function:
  160. ```js
  161. const MongoClient = require('mongodb').MongoClient;
  162. const assert = require('assert');
  163. // Connection URL
  164. const url = 'mongodb://localhost:27017';
  165. // Database Name
  166. const dbName = 'myproject';
  167. // Use connect method to connect to the server
  168. MongoClient.connect(url, function(err, client) {
  169. assert.equal(null, err);
  170. console.log("Connected successfully to server");
  171. const db = client.db(dbName);
  172. insertDocuments(db, function() {
  173. client.close();
  174. });
  175. });
  176. ```
  177. Run the updated **app.js** file:
  178. ```bash
  179. node app.js
  180. ```
  181. The operation returns the following output:
  182. ```bash
  183. Connected successfully to server
  184. Inserted 3 documents into the collection
  185. ```
  186. ### Find All Documents
  187. Add a query that returns all the documents.
  188. ```js
  189. const findDocuments = function(db, callback) {
  190. // Get the documents collection
  191. const collection = db.collection('documents');
  192. // Find some documents
  193. collection.find({}).toArray(function(err, docs) {
  194. assert.equal(err, null);
  195. console.log("Found the following records");
  196. console.log(docs)
  197. callback(docs);
  198. });
  199. }
  200. ```
  201. This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback:
  202. ```js
  203. const MongoClient = require('mongodb').MongoClient;
  204. const assert = require('assert');
  205. // Connection URL
  206. const url = 'mongodb://localhost:27017';
  207. // Database Name
  208. const dbName = 'myproject';
  209. // Use connect method to connect to the server
  210. MongoClient.connect(url, function(err, client) {
  211. assert.equal(null, err);
  212. console.log("Connected correctly to server");
  213. const db = client.db(dbName);
  214. insertDocuments(db, function() {
  215. findDocuments(db, function() {
  216. client.close();
  217. });
  218. });
  219. });
  220. ```
  221. ### Find Documents with a Query Filter
  222. Add a query filter to find only documents which meet the query criteria.
  223. ```js
  224. const findDocuments = function(db, callback) {
  225. // Get the documents collection
  226. const collection = db.collection('documents');
  227. // Find some documents
  228. collection.find({'a': 3}).toArray(function(err, docs) {
  229. assert.equal(err, null);
  230. console.log("Found the following records");
  231. console.log(docs);
  232. callback(docs);
  233. });
  234. }
  235. ```
  236. Only the documents which match ``'a' : 3`` should be returned.
  237. ### Update a document
  238. The following operation updates a document in the **documents** collection.
  239. ```js
  240. const updateDocument = function(db, callback) {
  241. // Get the documents collection
  242. const collection = db.collection('documents');
  243. // Update document where a is 2, set b equal to 1
  244. collection.updateOne({ a : 2 }
  245. , { $set: { b : 1 } }, function(err, result) {
  246. assert.equal(err, null);
  247. assert.equal(1, result.result.n);
  248. console.log("Updated the document with the field a equal to 2");
  249. callback(result);
  250. });
  251. }
  252. ```
  253. The method updates the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Next, update the callback function from **MongoClient.connect** to include the update method.
  254. ```js
  255. const MongoClient = require('mongodb').MongoClient;
  256. const assert = require('assert');
  257. // Connection URL
  258. const url = 'mongodb://localhost:27017';
  259. // Database Name
  260. const dbName = 'myproject';
  261. // Use connect method to connect to the server
  262. MongoClient.connect(url, function(err, client) {
  263. assert.equal(null, err);
  264. console.log("Connected successfully to server");
  265. const db = client.db(dbName);
  266. insertDocuments(db, function() {
  267. updateDocument(db, function() {
  268. client.close();
  269. });
  270. });
  271. });
  272. ```
  273. ### Remove a document
  274. Remove the document where the field **a** is equal to **3**.
  275. ```js
  276. const removeDocument = function(db, callback) {
  277. // Get the documents collection
  278. const collection = db.collection('documents');
  279. // Delete document where a is 3
  280. collection.deleteOne({ a : 3 }, function(err, result) {
  281. assert.equal(err, null);
  282. assert.equal(1, result.result.n);
  283. console.log("Removed the document with the field a equal to 3");
  284. callback(result);
  285. });
  286. }
  287. ```
  288. Add the new method to the **MongoClient.connect** callback function.
  289. ```js
  290. const MongoClient = require('mongodb').MongoClient;
  291. const assert = require('assert');
  292. // Connection URL
  293. const url = 'mongodb://localhost:27017';
  294. // Database Name
  295. const dbName = 'myproject';
  296. // Use connect method to connect to the server
  297. MongoClient.connect(url, function(err, client) {
  298. assert.equal(null, err);
  299. console.log("Connected successfully to server");
  300. const db = client.db(dbName);
  301. insertDocuments(db, function() {
  302. updateDocument(db, function() {
  303. removeDocument(db, function() {
  304. client.close();
  305. });
  306. });
  307. });
  308. });
  309. ```
  310. ### Index a Collection
  311. [Indexes](https://docs.mongodb.org/manual/indexes/) can improve your application's
  312. performance. The following function creates an index on the **a** field in the
  313. **documents** collection.
  314. ```js
  315. const indexCollection = function(db, callback) {
  316. db.collection('documents').createIndex(
  317. { "a": 1 },
  318. null,
  319. function(err, results) {
  320. console.log(results);
  321. callback();
  322. }
  323. );
  324. };
  325. ```
  326. Add the ``indexCollection`` method to your app:
  327. ```js
  328. const MongoClient = require('mongodb').MongoClient;
  329. const assert = require('assert');
  330. // Connection URL
  331. const url = 'mongodb://localhost:27017';
  332. const dbName = 'myproject';
  333. // Use connect method to connect to the server
  334. MongoClient.connect(url, function(err, client) {
  335. assert.equal(null, err);
  336. console.log("Connected successfully to server");
  337. const db = client.db(dbName);
  338. insertDocuments(db, function() {
  339. indexCollection(db, function() {
  340. client.close();
  341. });
  342. });
  343. });
  344. ```
  345. For more detailed information, see the [tutorials](docs/reference/content/tutorials/main.md).
  346. ## Next Steps
  347. * [MongoDB Documentation](http://mongodb.org)
  348. * [Read about Schemas](http://learnmongodbthehardway.com)
  349. * [Star us on GitHub](https://github.com/mongodb/node-mongodb-native)
  350. ## License
  351. [Apache 2.0](LICENSE.md)
  352. © 2009-2012 Christian Amor Kvalheim
  353. © 2012-present MongoDB [Contributors](CONTRIBUTORS.md)