Created
October 2, 2018 16:21
-
-
Save debojyoti/456f85d5c68369a5d8860fb3d35c0dfb 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
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 | |
} | |
}); | |
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