Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active April 13, 2022 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevinchisholm/a46fdf6a65931f00541de39d0021283e to your computer and use it in GitHub Desktop.
Save kevinchisholm/a46fdf6a65931f00541de39d0021283e to your computer and use it in GitHub Desktop.
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");
});
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();
});
});
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();
});
});
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);
});
});
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();
})
});
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