Skip to content

Commit

Permalink
Added MongoDB insertion examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanRobinson committed Aug 13, 2014
1 parent 30a90a1 commit 578e384
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CodeSamples/ch14/doc_add.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MongoClient.connect("mongodb://localhost/", function(err, db) {
type:"supernova",location:"Taurus"});
});
setTimeout(function(){ db.close(); }, 3000);
});
});
19 changes: 19 additions & 0 deletions Samples/MongoDB/doc_modify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var MongoClient = require('mongodb').MongoClient
MongoClient.connect("mongodb://localhost/", function(err, db) {
var myDB = db.db("astro")
myDB.collection("nebulae", function(err, nebulae) {
nebulae.find( {type: "supernova"}, function(err, items) {
items.toArray(function(err, itemArr) {
console.log("Before Modify: ");
console.log(itemArr);
nebulae.findAndModify({type: "supernova"}, [['name', 1]],
{$set: {type:"Super Nova", "updated":true}},
{w:1, new:true}, function(err, doc) {
console.log("After Modify: ");
console.log(doc);
db.close()
});
});
});
});
});
18 changes: 18 additions & 0 deletions Samples/MongoDB/doc_save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var MongoClient = require('mongodb').MongoClient
MongoClient.connect("mongodb://localhost/", function(err, db) {
var myDB = db.db("astro");
myDB.collection("nebulae", function(err, nebulae) {
nebulae.findOne({type: "Super Nova"}, function(err, item) {
console.log("Before Save: ");
console.log(item);
item.info = "Some new info";
nebulae.save(item, {w:1}, function(err, results) {
nebulae.findOne({_id: item._id}, function(err, savedItem) {
console.log("After Save: ");
console.log(savedItem);
db.close();
});
});
});
});
});
36 changes: 36 additions & 0 deletions Samples/MongoDB/doc_upsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var MongoClient = require('mongodb').MongoClient
MongoClient.connect("mongodb://localhost/", function(err, db) {
var myDB = db.db("astro");
myDB.collection("nebulae", function(err, nebulae) {
nebulae.find({type: "diffuse"}, function(err, items) {
items.toArray(function(err, itemArr) {
console.log("Before Upsert: ");
console.log(itemArr);
nebulae.update({type: "diffuse"},
{$set: {ngc: "NGC 3372", name: "Carina",
type: "diffuse", lcoation: "Carina"}},
{upsert: true, w:1, forceServerObjectId: false},
function(err, results) {
nebulae.find({type: "diffuse"}, function(err, items) {
items.toArray(function(err, itemArr) {
console.log("After Upsert 1: ");
console.log(itemArr);
var itemID = itemArr[0]._id;
nebulae.update({_id: itemID},
{$set: {ngc: "NGC 3372", name: "Carina",
type: "Diffuse", location: "Carina"}},
{upsert: true, w:1}, function(err, results) {
nebulae.findOne({_id: itemID}, function(err, item) {
console.log("After Upsert 2: ");
console.log(item);
db.close();
});
});
});
});
});
});
});
});
});

0 comments on commit 578e384

Please sign in to comment.