Skip to content

Commit 434efd8

Browse files
author
Ryan Kotzen
committed
more mongodb demos
1 parent 166d15f commit 434efd8

File tree

10 files changed

+396
-92
lines changed

10 files changed

+396
-92
lines changed

.idea/inspectionProfiles/Project_Default.xml

-6
This file was deleted.

.idea/workspace.xml

+257-68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ A repository for my Beer & Technology talk on MongoDB and Node.js
1313
* https://www.youtube.com/watch?v=bzkRVzciAZg
1414
* http://www.mongodb-is-web-scale.com/
1515

16-
#TODO
16+
# Possible addons
1717

1818
* eslint
1919
* testing (mocking, async vs sync)
2020

21-
* MongoDB
22-
* MongoChef
23-
2421
* How to deal with async callback hell (Promise, Async.js, async/await co/generator functions)
2522

2623
* Express

node/mongodb/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Installation
2+
3+
https://www.mongodb.com/download-center?jmp=nav
4+
5+
Running as a service: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-edition
6+
7+
# Api Reference
8+
9+
To see all the possible methods and what they accept/return, see [the api docs.](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html)
10+
11+
# Using the shell
12+
13+
Ensure that mongo is running and is in your path variable (e.g. `C:\Program Files\MongoDB\Server\3.2\bin`). Open up a terminal and type `mongo`
14+
15+
> `use <nameOfDb>`
16+
> `db.test.insertOne({asd:true})`
17+
18+
Simple as that!
19+
20+
# Using some more advanced tools
21+
22+
Download and install [MongoChef](https://studio3t.com/download/) it's like the sql management studio of the mongo world.
23+
[RoboMongo](https://robomongo.org/) is a great alternative if you aren't working with a replica set.
24+

node/mongodb/collection-names.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
example: "example"
5+
};

node/mongodb/create.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
'use strict';
22
const mongo = require('./mongo');
3-
3+
const mongodb = require('mongodb');
4+
const collectionNames = require('./collection-names');
45
mongo.connect(connected);
6+
57
function connected(err, db) {
68
if (err) {
79
throw err;
810
}
9-
db.collection('example').insertOne({value: 1});
11+
const exampleDocument = {
12+
naturalKey: "bob",
13+
value: 1,
14+
dateCreated: new Date(),
15+
owner: new mongodb.ObjectId(),
16+
someCoolSubObject: {
17+
isCool: true
18+
}
19+
};
20+
db.collection(collectionNames.example).insertOne(exampleDocument, inserted);
21+
}
22+
23+
function inserted(err, result) {
24+
if (err) {
25+
mongo.close(disconnected);
26+
throw err;
27+
}
28+
console.log(result);
1029
mongo.close(disconnected);
1130
}
1231

node/mongodb/delete.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const mongo = require('./mongo');
3+
const collectionNames = require('./collection-names');
4+
mongo.connect(connected);
5+
6+
function connected(err, db) {
7+
if (err) {
8+
throw err;
9+
}
10+
const filter = {naturalKey: "bob"};
11+
db.collection(collectionNames.example).findOneAndDelete(filter, deleted);
12+
}
13+
14+
function deleted(err, result) {
15+
if (err) {
16+
mongo.close(disconnected);
17+
throw err;
18+
}
19+
console.log(result);
20+
mongo.close(disconnected);
21+
}
22+
23+
function disconnected(err) {
24+
if (err) {
25+
throw err;
26+
}
27+
console.log("Exiting");
28+
}

node/mongodb/mongo.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
'use strict';
22
const mongo = require('mongodb');
33
const connectionString = "mongodb://localhost:27017/beer-and-tech";
4-
const opts = {
5-
socketOptions: {
6-
keepAlive: 1,
7-
autoReconnect: true
8-
},
9-
sslValidate: false
10-
};
11-
const mongoConnectionOptions = {
12-
server: opts,
13-
replSet: opts
14-
};
154
let state = {
165
connect: connectToDb,
176
db: null,
@@ -43,7 +32,7 @@ function connectToDb(callback) {
4332
callback(null, state.db);
4433
});
4534
}
46-
mongo.MongoClient.connect(connectionString, mongoConnectionOptions, connected);
35+
mongo.MongoClient.connect(connectionString, connected);
4736
function connected(err, db) {
4837
if (err) {
4938
return callback(err);

node/mongodb/retrieve.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const mongo = require('./mongo');
3+
const collectionNames = require('./collection-names');
4+
mongo.connect(connected);
5+
6+
function connected(err, db) {
7+
if (err) {
8+
throw err;
9+
}
10+
db.collection(collectionNames.example).findOne({naturalKey: "bob"}, retrieved);
11+
}
12+
13+
function retrieved(err, result) {
14+
if (err) {
15+
mongo.close(disconnected);
16+
throw err;
17+
}
18+
console.log(result);
19+
mongo.close(disconnected);
20+
}
21+
22+
function disconnected(err) {
23+
if (err) {
24+
throw err;
25+
}
26+
console.log("Exiting");
27+
}

node/mongodb/update.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
const mongo = require('./mongo');
3+
const collectionNames = require('./collection-names');
4+
mongo.connect(connected);
5+
6+
function connected(err, db) {
7+
if (err) {
8+
throw err;
9+
}
10+
const filter = {naturalKey: "bob"};
11+
const replacement = {
12+
naturalKey: "bob",
13+
updated: true
14+
};
15+
db.collection(collectionNames.example).findOneAndReplace(filter, replacement, replaced);
16+
}
17+
18+
function replaced(err, result) {
19+
if (err) {
20+
mongo.close(disconnected);
21+
throw err;
22+
}
23+
console.log(result);
24+
mongo.close(disconnected);
25+
}
26+
27+
function disconnected(err) {
28+
if (err) {
29+
throw err;
30+
}
31+
console.log("Exiting");
32+
}

0 commit comments

Comments
 (0)