npm install mongodb --save
const MongoClient = require('mongodb').MongoClient;
// Connect to the db mongodb://<user>:<password>@<host>:<port>/<databasename>
MongoClient.connect(
"mongodb://ggm:[email protected]:17758/chatbot-2018",
function(err, client) {
if (err) {
return console.dir(err);
}
db = client.db('chatbot-2018');
}
);
let collection = db.collection('test');
let doc1 = {'hello':'doc1'};
let doc2 = {'hello':'doc2'};
let lotsOfDocs = [{'hello':'doc3'}, {'hello':'doc4'}];
collection.insert(doc1);
collection.insert(doc2, {w:1}, function(err, result) {});
collection.insert(lotsOfDocs, {w:1}, function(err, result) {});
let collection = db.collection('test');
let cursor = collection.find({});
cursor.nextObject((err, item) => {
console.log(item)
})
collection.find({}, (err, cursor) => {
cursor.nextObject((err, item) => {
console.log(item)
})
})
let collection = db.collection('test');
collection.find({}).toArray((err, results) => {
console.log(results)
});