Skip to content

Instantly share code, notes, and snippets.

@bao-qian
Last active February 22, 2017 18:38
Show Gist options
  • Save bao-qian/92d81e09de09bd297b4694da2968327a to your computer and use it in GitHub Desktop.
Save bao-qian/92d81e09de09bd297b4694da2968327a to your computer and use it in GitHub Desktop.
mongodb pooling
const express = require('express');
const app = express();
const db = require('./database.js');
app.listen(3000);
console.log("Listening on port 3000");
app.get("/", (req, res) =>{
db.query().then(docs =>{
res.send(docs);
});
});
const mongodb = require('mongodb');
let db;
init();
function init() {
const url = 'mongodb://127.0.0.1:27017/test';
mongodb.MongoClient.connect(url).then((initialized) => {
db = initialized;
});
}
exports.query = function () {
let p = db.collection('restaurants').find({}).toArray();
return p;
};
> use test
switched to db test
> db.restaurants.insert(
... {
... "address" : {
... "street" : "2 Avenue",
... "zipcode" : "10075",
... "building" : "1480",
... "coord" : [ -73.9557413, 40.7720266 ]
... },
... "borough" : "Manhattan",
... "cuisine" : "Italian",
... "grades" : [
... {
... "date" : ISODate("2014-10-01T00:00:00Z"),
... "grade" : "A",
... "score" : 11
... },
... {
... "date" : ISODate("2014-01-16T00:00:00Z"),
... "grade" : "B",
... "score" : 17
... }
... ],
... "name" : "Vella",
... "restaurant_id" : "41704620"
... }
... )
WriteResult({ "nInserted" : 1 })
> db.restaurants.find().toArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment