Skip to content

Commit 9a9e161

Browse files
committed
add gitignore which ignores node_modules folder in future
add two simple node and mongo use case
1 parent 085c6ae commit 9a9e161

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

node_mongo/simple-cli.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
var MongoClient = require('mongodb').MongoClient
2+
3+
// move connecting into a function to avoid the 'pyramid of doom'
4+
function getConnection(cb) {
5+
MongoClient.connect('mongodb://localhost', function(err, db) {
6+
if (err) return cb(err);
7+
8+
var simpleUser = db.collection('simple');
9+
10+
// to search by name, index is need to get speed
11+
// ref: http://docs.mongodb.org/manual/core/indexes-introduction/
12+
simpleUser.ensureIndex({name: true}, function(err) {
13+
if (err) return cb(err);
14+
cb(null, simpleUser);
15+
});
16+
});
17+
}
18+
19+
// an upsert will create a new record OR update an existing record
20+
// which makes things easier, in mongo, we can do this with a
21+
// findAndModify and passing the upsert option to have the update
22+
// document returned, we pass the new option as well
23+
function upsertUser(collection, name, role, cb) {
24+
collection.findAndModify({name: name}, {}, {$set: {role: role}},
25+
{upsert: true, new: true}, cb);
26+
}
27+
28+
// instead of finding just one user, we can list all of the documents
29+
// by passing an empty selector. This returns a 'cursor', which allows
30+
// us to walk through the documents look at how we do this in process
31+
function readAll(collection, cb) {
32+
collection.find({}, cb);
33+
}
34+
35+
function readRole(collection, name, cb) {
36+
collection.findOne({name: name}, cb);
37+
}
38+
39+
function printUser(user) {
40+
// make sure that we found our user
41+
if (!user) {
42+
console.log("Couldn't find the user you asked for!")
43+
}
44+
45+
console.log(user.name + ' has the role of ' + user.role);
46+
}
47+
48+
// the each method allows us to walk through the result set, notice
49+
// the callback, as every time the callback is called, there is
50+
// another chance of an error
51+
function printUsers(users, cb) {
52+
users.each(function(err, user) {
53+
if (err) return cb(err);
54+
printUser(user);
55+
});
56+
}
57+
58+
function simpleCli(operation, name, role, cb) {
59+
getConnection(function(err, collection) {
60+
if (err) return cb(err);
61+
62+
// we need to make sure to close the database, otherwise
63+
// the process won't stop
64+
function processUser(err, user) {
65+
if (err) return cb(user);
66+
printUser(user);
67+
collection.db.close();
68+
cb();
69+
}
70+
71+
// use this function when dealing with lots of users
72+
function processUsers(err, users) {
73+
if (err) return cb(err);
74+
// the callback to each is called for every result
75+
// once it returns a null, we know the result is done
76+
users.each(function(err, user) {
77+
if (err) return cb(err);
78+
if (user) {
79+
printUser(user);
80+
} else {
81+
collection.db.close();
82+
cb();
83+
}
84+
});
85+
}
86+
87+
if (operation === 'list') {
88+
readAll(collection, processUsers);
89+
} else if (operation === 'update') {
90+
upsertUser(collection, name, role, processUser);
91+
} else if (operation === 'read') {
92+
readRole(collection, name, processUser);
93+
} else {
94+
return cb(new Error('unknown operation!'));
95+
}
96+
});
97+
}
98+
99+
var operation = process.argv[2];
100+
var name = process.argv[3];
101+
var role = process.argv[4];
102+
103+
simpleCli(operation, name, role, function(err) {
104+
if (err) {
105+
console.log("Had an error!", err);
106+
process.exit(1);
107+
}
108+
});

node_mongo/simple.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// import the mongo driver that help connect to mongo
2+
var MongoClient = require('mongodb').MongoClient
3+
4+
MongoClient.connect('mongodb://localhost', function(err, db) {
5+
// if we didn't connect, throw the error
6+
if (err) throw err;
7+
8+
// in mongo, documents are grouped in collection (like a table)
9+
// make one
10+
var simpleUser = db.collection("simple");
11+
12+
// inserting a new document of easy json
13+
simpleUser.insert({name: 'ccppjava', role: 'admin'}, function(err, result) {
14+
if (err) throw err;
15+
16+
// all documents in mongo get assigned a unique id, _id
17+
// we use this to find the document we just inserted
18+
var _id = result[0]._id
19+
20+
// to update, we write a 'selector', and then the update
21+
// notice the use of $set, it is a special operator that
22+
// sets the fields in the document, otherwise, we would
23+
// wipe out the exisitng document
24+
simpleUser.update({_id: _id}, {$set: {role: 'user'}}, function(err){
25+
if (err) throw err;
26+
27+
// finding a documents needs a selector like above
28+
simpleUser.findOne({_id: _id}, function(err, doc) {
29+
if (err) throw err;
30+
31+
console.log(doc.name + " has the role of " + doc.role);
32+
// close our database so the process will die
33+
db.close();
34+
});
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)