Created
October 2, 2018 15:51
-
-
Save debojyoti/1d654805427a41ad173c83cd8c01ea57 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
/** | |
* Basic script to start with | |
* mongodb (Connection steps | |
* and Read operation) | |
*/ | |
// Step-1: Get the MongoClient (Driver for node) | |
const MongoClient = require("mongodb").MongoClient; | |
// Step-2: Store mongo server url | |
let url = 'mongodb://localhost:27017'; | |
// Step-5: Custom function | |
const getAllData = function(client, callback) { | |
// Step-6: Select db | |
let db = client.db("analytics"); | |
// Step-7: Select collection | |
let collection = db.collection("sites"); | |
// Step-8: Call functions on the collection | |
collection.find({ | |
"domain": "debojyoti.xyz" | |
}).toArray((err, docs) => { | |
console.log(docs); | |
callback(); | |
}) | |
} | |
const config = { | |
useNewUrlParser: true | |
}; | |
// Step-3: Call connect using the client | |
MongoClient.connect(url, config, (err, client) => { | |
// Step-4: Send the client instance to custom function | |
getAllData(client, () => { | |
console.log("Closing db connection"); | |
client.close(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment