Last active
April 13, 2022 17:43
-
-
Save kevinchisholm/a46fdf6a65931f00541de39d0021283e 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
var mongoDbClient = require('mongodb').MongoClient, | |
dbUrl = 'mongodb://localhost:27017', | |
dbName = 'madMen', | |
collectionName = 'names'; | |
//connect to the madMen database | |
mongoDbClient.connect(dbUrl, function(err, database) { | |
console.log("Connected successfully to MongoDB database"); | |
}); |
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
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
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
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
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