Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active January 12, 2018 22:25
Show Gist options
  • Save kevinchisholm/2c0e835d03c79c1d15898c8ba5d85380 to your computer and use it in GitHub Desktop.
Save kevinchisholm/2c0e835d03c79c1d15898c8ba5d85380 to your computer and use it in GitHub Desktop.
//switch to the madMen database
use madMen
//insert one document
db.names.insert({'name' : 'Don Draper'});
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();
});
});
//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'}
]);
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();
});
});
//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" }
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);
});
});
db.names.remove({_id:ObjectId('xxxx1')});
db.names.remove({_id:ObjectId('xxxx2')});
db.names.remove({_id:ObjectId('xxxx3')});
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();
})
});
//remove all documents
//in the names collection
db.names.remove({});
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();
})
});
//remove all documents
//in the names collection
db.names.remove({});
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