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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. ### Support / Feedback
  24. For issues with, questions about, or feedback for the Node.js driver, please look into our [support channels](http://www.mongodb.org/about/support). Please do not email any of the driver developers directly with issues or questions - you're more likely to get an answer on the [mongodb-user](http://groups.google.com/group/mongodb-user>) list on Google Groups.
  25. ### Change Log
  26. Change history can be found in [`HISTORY.md`](HISTORY.md).
  27. ### Compatibility
  28. For version compatibility matrices, please refer to the following links:
  29. * [MongoDB](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-node)
  30. * [NodeJS](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-language-node)
  31. # Installation
  32. 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.
  33. ## MongoDB Driver
  34. 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.
  35. ```bash
  36. npm install mongodb --save
  37. ```
  38. This will download the MongoDB driver and add a dependency entry in your `package.json` file.
  39. You can also use the [Yarn](https://yarnpkg.com/en) package manager.
  40. ## Troubleshooting
  41. The MongoDB driver depends on several other packages. These are:
  42. * [mongodb-core](https://github.com/mongodb-js/mongodb-core)
  43. * [bson](https://github.com/mongodb/js-bson)
  44. * [kerberos](https://github.com/mongodb-js/kerberos)
  45. * [node-gyp](https://github.com/nodejs/node-gyp)
  46. 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.
  47. **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.**
  48. ### Diagnosing on UNIX
  49. 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.
  50. ```bash
  51. git clone https://github.com/mongodb-js/kerberos
  52. cd kerberos
  53. npm install
  54. ```
  55. 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:
  56. ```bash
  57. npm install -g node-gyp
  58. ```
  59. 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.
  60. ```bash
  61. cd yourproject
  62. npm install mongodb --save
  63. ```
  64. If it still fails the next step is to examine the npm log. Rerun the command but in this case in verbose mode.
  65. ```bash
  66. npm --loglevel verbose install mongodb
  67. ```
  68. This will print out all the steps npm is performing while trying to install the module.
  69. ### Diagnosing on Windows
  70. A compiler tool chain known to work for compiling `kerberos` on Windows is the following.
  71. * Visual Studio C++ 2010 (do not use higher versions)
  72. * Windows 7 64bit SDK
  73. * Python 2.7 or higher
  74. Open the Visual Studio command prompt. Ensure `node.exe` is in your path and install `node-gyp`.
  75. ```bash
  76. npm install -g node-gyp
  77. ```
  78. Next, you will have to build the project manually to test it. Clone the repo, install dependencies and rebuild:
  79. ```bash
  80. git clone https://github.com/christkv/kerberos.git
  81. cd kerberos
  82. npm install
  83. node-gyp rebuild
  84. ```
  85. This should rebuild the driver successfully if you have everything set up correctly.
  86. ### Other possible issues
  87. 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).
  88. Another tip is to ensure your user has write permission to wherever the Node.js modules are being installed.
  89. ## Quick Start
  90. 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).
  91. ### Create the `package.json` file
  92. First, create a directory where your application will live.
  93. ```bash
  94. mkdir myproject
  95. cd myproject
  96. ```
  97. Enter the following command and answer the questions to create the initial structure for your new project:
  98. ```bash
  99. npm init
  100. ```
  101. Next, install the driver dependency.
  102. ```bash
  103. npm install mongodb --save
  104. ```
  105. 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.
  106. ### Start a MongoDB Server
  107. For complete MongoDB installation instructions, see [the manual](https://docs.mongodb.org/manual/installation/).
  108. 1. Download the right MongoDB version from [MongoDB](https://www.mongodb.org/downloads)
  109. 2. Create a database directory (in this case under **/data**).
  110. 3. Install and start a ``mongod`` process.
  111. ```bash
  112. mongod --dbpath=/data
  113. ```
  114. You should see the **mongod** process start up and print some status information.
  115. ### Connect to MongoDB
  116. Create a new **app.js** file and add the following code to try out some basic CRUD
  117. operations using the MongoDB driver.
  118. Add code to connect to the server and the database **myproject**:
  119. ```js
  120. const MongoClient = require('mongodb').MongoClient;
  121. const assert = require('assert');
  122. // Connection URL
  123. const url = 'mongodb://localhost:27017';
  124. // Database Name
  125. const dbName = 'myproject';
  126. // Use connect method to connect to the server
  127. MongoClient.connect(url, function(err, client) {
  128. assert.equal(null, err);
  129. console.log("Connected successfully to server");
  130. const db = client.db(dbName);
  131. client.close();
  132. });
  133. ```
  134. Run your app from the command line with:
  135. ```bash
  136. node app.js
  137. ```
  138. The application should print **Connected successfully to server** to the console.
  139. ### Insert a Document
  140. Add to **app.js** the following function which uses the **insertMany**
  141. method to add three documents to the **documents** collection.
  142. ```js
  143. const insertDocuments = function(db, callback) {
  144. // Get the documents collection
  145. const collection = db.collection('documents');
  146. // Insert some documents
  147. collection.insertMany([
  148. {a : 1}, {a : 2}, {a : 3}
  149. ], function(err, result) {
  150. assert.equal(err, null);
  151. assert.equal(3, result.result.n);
  152. assert.equal(3, result.ops.length);
  153. console.log("Inserted 3 documents into the collection");
  154. callback(result);
  155. });
  156. }
  157. ```
  158. The **insert** command returns an object with the following fields:
  159. * **result** Contains the result document from MongoDB
  160. * **ops** Contains the documents inserted with added **_id** fields
  161. * **connection** Contains the connection used to perform the insert
  162. Add the following code to call the **insertDocuments** function:
  163. ```js
  164. const MongoClient = require('mongodb').MongoClient;
  165. const assert = require('assert');
  166. // Connection URL
  167. const url = 'mongodb://localhost:27017';
  168. // Database Name
  169. const dbName = 'myproject';
  170. // Use connect method to connect to the server
  171. MongoClient.connect(url, function(err, client) {
  172. assert.equal(null, err);
  173. console.log("Connected successfully to server");
  174. const db = client.db(dbName);
  175. insertDocuments(db, function() {
  176. client.close();
  177. });
  178. });
  179. ```
  180. Run the updated **app.js** file:
  181. ```bash
  182. node app.js
  183. ```
  184. The operation returns the following output:
  185. ```bash
  186. Connected successfully to server
  187. Inserted 3 documents into the collection
  188. ```
  189. ### Find All Documents
  190. Add a query that returns all the documents.
  191. ```js
  192. const findDocuments = function(db, callback) {
  193. // Get the documents collection
  194. const collection = db.collection('documents');
  195. // Find some documents
  196. collection.find({}).toArray(function(err, docs) {
  197. assert.equal(err, null);
  198. console.log("Found the following records");
  199. console.log(docs)
  200. callback(docs);
  201. });
  202. }
  203. ```
  204. This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback:
  205. ```js
  206. const MongoClient = require('mongodb').MongoClient;
  207. const assert = require('assert');
  208. // Connection URL
  209. const url = 'mongodb://localhost:27017';
  210. // Database Name
  211. const dbName = 'myproject';
  212. // Use connect method to connect to the server
  213. MongoClient.connect(url, function(err, client) {
  214. assert.equal(null, err);
  215. console.log("Connected correctly to server");
  216. const db = client.db(dbName);
  217. insertDocuments(db, function() {
  218. findDocuments(db, function() {
  219. client.close();
  220. });
  221. });
  222. });
  223. ```
  224. ### Find Documents with a Query Filter
  225. Add a query filter to find only documents which meet the query criteria.
  226. ```js
  227. const findDocuments = function(db, callback) {
  228. // Get the documents collection
  229. const collection = db.collection('documents');
  230. // Find some documents
  231. collection.find({'a': 3}).toArray(function(err, docs) {
  232. assert.equal(err, null);
  233. console.log("Found the following records");
  234. console.log(docs);
  235. callback(docs);
  236. });
  237. }
  238. ```
  239. Only the documents which match ``'a' : 3`` should be returned.
  240. ### Update a document
  241. The following operation updates a document in the **documents** collection.
  242. ```js
  243. const updateDocument = function(db, callback) {
  244. // Get the documents collection
  245. const collection = db.collection('documents');
  246. // Update document where a is 2, set b equal to 1
  247. collection.updateOne({ a : 2 }
  248. , { $set: { b : 1 } }, function(err, result) {
  249. assert.equal(err, null);
  250. assert.equal(1, result.result.n);
  251. console.log("Updated the document with the field a equal to 2");
  252. callback(result);
  253. });
  254. }
  255. ```
  256. 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.
  257. ```js
  258. const MongoClient = require('mongodb').MongoClient;
  259. const assert = require('assert');
  260. // Connection URL
  261. const url = 'mongodb://localhost:27017';
  262. // Database Name
  263. const dbName = 'myproject';
  264. // Use connect method to connect to the server
  265. MongoClient.connect(url, function(err, client) {
  266. assert.equal(null, err);
  267. console.log("Connected successfully to server");
  268. const db = client.db(dbName);
  269. insertDocuments(db, function() {
  270. updateDocument(db, function() {
  271. client.close();
  272. });
  273. });
  274. });
  275. ```
  276. ### Remove a document
  277. Remove the document where the field **a** is equal to **3**.
  278. ```js
  279. const removeDocument = function(db, callback) {
  280. // Get the documents collection
  281. const collection = db.collection('documents');
  282. // Delete document where a is 3
  283. collection.deleteOne({ a : 3 }, function(err, result) {
  284. assert.equal(err, null);
  285. assert.equal(1, result.result.n);
  286. console.log("Removed the document with the field a equal to 3");
  287. callback(result);
  288. });
  289. }
  290. ```
  291. Add the new method to the **MongoClient.connect** callback function.
  292. ```js
  293. const MongoClient = require('mongodb').MongoClient;
  294. const assert = require('assert');
  295. // Connection URL
  296. const url = 'mongodb://localhost:27017';
  297. // Database Name
  298. const dbName = 'myproject';
  299. // Use connect method to connect to the server
  300. MongoClient.connect(url, function(err, client) {
  301. assert.equal(null, err);
  302. console.log("Connected successfully to server");
  303. const db = client.db(dbName);
  304. insertDocuments(db, function() {
  305. updateDocument(db, function() {
  306. removeDocument(db, function() {
  307. client.close();
  308. });
  309. });
  310. });
  311. });
  312. ```
  313. ### Index a Collection
  314. [Indexes](https://docs.mongodb.org/manual/indexes/) can improve your application's
  315. performance. The following function creates an index on the **a** field in the
  316. **documents** collection.
  317. ```js
  318. const indexCollection = function(db, callback) {
  319. db.collection('documents').createIndex(
  320. { "a": 1 },
  321. null,
  322. function(err, results) {
  323. console.log(results);
  324. callback();
  325. }
  326. );
  327. };
  328. ```
  329. Add the ``indexCollection`` method to your app:
  330. ```js
  331. const MongoClient = require('mongodb').MongoClient;
  332. const assert = require('assert');
  333. // Connection URL
  334. const url = 'mongodb://localhost:27017';
  335. const dbName = 'myproject';
  336. // Use connect method to connect to the server
  337. MongoClient.connect(url, function(err, client) {
  338. assert.equal(null, err);
  339. console.log("Connected successfully to server");
  340. const db = client.db(dbName);
  341. insertDocuments(db, function() {
  342. indexCollection(db, function() {
  343. client.close();
  344. });
  345. });
  346. });
  347. ```
  348. For more detailed information, see the [tutorials](docs/reference/content/tutorials/main.md).
  349. ## Next Steps
  350. * [MongoDB Documentation](http://mongodb.org)
  351. * [Read about Schemas](http://learnmongodbthehardway.com)
  352. * [Star us on GitHub](https://github.com/mongodb/node-mongodb-native)
  353. ## License
  354. [Apache 2.0](LICENSE.md)
  355. © 2009-2012 Christian Amor Kvalheim
  356. © 2012-present MongoDB [Contributors](CONTRIBUTORS.md)