Skip to content

Commit 8ab3b07

Browse files
author
Jason Yang
committedNov 10, 2015
Merge pull request #11 from tstringer/master
Added MongoDB information
2 parents 70b6a54 + 8181b95 commit 8ab3b07

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed
 

‎README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,48 @@ Sometimes, when deploying a native module to production, oftentimes it is not po
280280

281281
## Application-specific tips and tricks
282282
> :triangular_flag_on_post: **TODO**
283-
* Setting up MongoDB.
284283
* Setting up SQL.
285284
* .NET in-process using Edge.js.
286285
* [node-windows](https://github.com/coreybutler/node-windows): Windows services, logging, and commands using Node.js.
287286

287+
### Setting up and working with MongoDB
288+
289+
1. [Install MongoDB on Windows](https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/)
290+
2. Install the MongoDB npm package in the project root by running `npm install mongodb`
291+
3. *(Optional, but helpful for IntelliSense support in Visual Studio Code)* Install the type definition file for MongoDB with `tsd install mongodb`
292+
4. Load the mongodb module: `var MongoClient = require('mongodb').MongoClient;`
293+
5. [Determine your MongoDB connection string URI](https://docs.mongodb.org/manual/reference/connection-string/) and set it to a variable: `var mongoUrl = '...'`;
294+
> Example: mongodb://localhost:27017/yourDatabaseName
295+
296+
> :bulb: MongoDB by default listens on 27017, but to verify this open the MongoDB log file `mongod.log` in the log directory and navigate the line that shows the port number (i.e. `[initandlisten] waiting for connections on port 27017`)
297+
6. Access your MongoDB database by calling `MongoClient.connect()` and query the returned database
298+
```javascript
299+
MongoClient.connect(mongoUrl, function (err, db) {
300+
if (!err) {
301+
// query the collection and return a cursor to use
302+
// to access the data
303+
//
304+
// note: calling find() with no parameters is the equivalent
305+
// of pulling all documents in the collection with no
306+
// predicate
307+
var cursor = db.collection('yourCollectionName').find();
308+
309+
// loop through all of the documents returned by the
310+
// query
311+
cursor.each(function (err, element) {
312+
if (!err && element) {
313+
// do something with the document
314+
}
315+
// if error and the document are undefined then
316+
// we have reached the no-more-documents condition
317+
if (!err && !element) {
318+
console.log('done!');
319+
}
320+
});
321+
}
322+
});
323+
```
324+
288325
## Node.js + Microsoft products, services, and contributions
289326
* [**Visual Studio Code**](https://code.visualstudio.com/): Lightweight cross-platform editor for building and debugging modern web and cloud applications.
290327
* [**Node.js Tools for Visual Studio**](https://www.visualstudio.com/features/node-js-vs): Free, open-source extension that turns Visual Studio into a powerful Node.js development environment.

0 commit comments

Comments
 (0)