Last active
January 12, 2018 22:25
-
-
Save kevinchisholm/2c0e835d03c79c1d15898c8ba5d85380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//switch to the madMen database | |
use madMen | |
//insert one document | |
db.names.insert({'name' : 'Don Draper'}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoDbClient = require('mongodb').MongoClient, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names', | |
dbDocument = {name : 'Don Draper'}; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
console.log("Connected successfully to MongoDB database"); | |
//set the database and collection references | |
var db = database.db(dbName), | |
collection = db.collection(collectionName); | |
//insert one document into the collection | |
collection.insert(dbDocument, function(err, result) { | |
console.log("Inserted 1 document into the collection:"); | |
console.log(result); | |
//close the database | |
database.close(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//insert multiple documents - one at a time | |
db.names.insert({'name' : 'Don Draper'}); | |
db.names.insert({'name' : 'Pete Campbell'}); | |
db.names.insert({'name' : 'Joan Harris'}); | |
//insert multiple documents - using insertMany | |
db.names.insertMany([ | |
{name : 'Peggy Olson'}, | |
{name : 'Roger Sterling'}, | |
{name : 'Bert Cooper'} | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoDbClient = require('mongodb').MongoClient, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names', | |
dbDocuments = [ | |
{name : 'Don Draper'}, | |
{name : 'Pete Campbell'}, | |
{name : 'Joan Harris'} | |
]; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
console.log("Connected successfully to MongoDB database"); | |
//set the database and collection references | |
var db = database.db(dbName), | |
collection = db.collection(collectionName); | |
//insert one document into the collection | |
collection.insertMany(dbDocuments, function(err, result) { | |
console.log('Inserted ' + dbDocuments.length + ' documents into the collection:'); | |
console.log(result); | |
//close the database | |
database.close(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//show all records in the database | |
db.names.find(); | |
//output | |
{ "_id" : ObjectId("xxxx1"), "name" : "Don Draper" } | |
{ "_id" : ObjectId("xxxx2"), "name" : "Pete Campbell" } | |
{ "_id" : ObjectId("xxxx3"), "name" : "Joan Harris" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoDbClient = require('mongodb').MongoClient, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names'; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
database | |
.db(dbName) | |
.collection(collectionName) | |
.find() | |
.each(function (err, document) { | |
//if the document is null | |
if (document == null) { | |
//close the database and return | |
database.close(); | |
return; | |
} | |
//show the next document | |
console.log(document); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.names.remove({_id:ObjectId('xxxx1')}); | |
db.names.remove({_id:ObjectId('xxxx2')}); | |
db.names.remove({_id:ObjectId('xxxx3')}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoDbClient = require('mongodb').MongoClient, | |
ObjectID = require('mongodb').ObjectID, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names', | |
documentToDelete = {_id: new ObjectID('XXXXX')}; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
database | |
database | |
.db(dbName) | |
.collection(collectionName) | |
.deleteOne(documentToDelete) | |
.then(function(result) { | |
console.log('Database record deleted'); | |
//close the database and return | |
database.close(); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//remove all documents | |
//in the names collection | |
db.names.remove({}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoDbClient = require('mongodb').MongoClient, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names'; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
database | |
database | |
.db(dbName) | |
.collection(collectionName) | |
.deleteMany({}) | |
.then(function(result) { | |
console.log('All database records deleted'); | |
//close the database and return | |
database.close(); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//remove all documents | |
//in the names collection | |
db.names.remove({}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoDbClient = require('mongodb').MongoClient, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names'; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
database | |
database | |
.db(dbName) | |
.collection(collectionName) | |
.deleteMany({}) | |
.then(function(result) { | |
console.log('All database records deleted'); | |
//close the database and return | |
database.close(); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment