Skip to content

Instantly share code, notes, and snippets.

@debojyoti
Created October 2, 2018 16:21
Show Gist options
  • Save debojyoti/456f85d5c68369a5d8860fb3d35c0dfb to your computer and use it in GitHub Desktop.
Save debojyoti/456f85d5c68369a5d8860fb3d35c0dfb to your computer and use it in GitHub Desktop.
const MongoClient = require("mongodb").MongoClient;
const MongoServerUrl = "mongodb://localhost:27017";
const Config = {
useNewUrlParser: true
};
MongoClient.connect(MongoServerUrl, Config, (err, client) => {
MongoCrud(client, () => {
client.close();
})
});
function MongoCrud(client, callback) {
let db = client.db("analytics");
let collection = db.collection("sites");
// Insert a new site
collection.insertOne({
"domain": "http://accounts.debojyoti.xyz"
});
// Display all sites
collection.find().toArray((err, docs) => {
console.log(docs);
});
// Remove some docs
collection.deleteOne({
"domain": "http://accounts.debojyoti.xyz"
});
// Print available sites again
collection.find().toArray((err, docs) => {
console.log(docs);
});
// Update
collection.updateOne({
"domain": "https://accounts.debojyoti.xyz"
}, {
$set: {
"domain": "http://accounts.debojyoti.xyz"
},
$currentDate: {
lastModified: true
}
});
// Print
collection.find().toArray((err, docs) => {
console.log(docs);
callback();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment