SHARE
TWEET

Untitled

a guest Apr 25th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const express = require('express');
  3. const app = express()
  4. var MongoClient = require('mongodb').MongoClient;
  5. var assert = require('assert');
  6. var ObjectId = require('mongodb').ObjectID;
  7. var url = 'XXXXXXXX';
  8.  
  9. var client;
  10.  
  11. // GET /families
  12. app.get('/families', (req, res) => {
  13.     var collection = client.collection('families')
  14.     collection.find().toArray(function(err, families)) {
  15.         if(err) {
  16.             res.status(500).send(err)
  17.             return
  18.         }
  19.  
  20.         res.json(families)
  21.     }
  22. })
  23.  
  24. // Connect to db and start server
  25. MongoClient.connect(url, function(err, connectedClient) {
  26.     if (err) {
  27.         console.log('Unable to connect to Mongo.')
  28.         process.exit(1)
  29.     }
  30.  
  31.     client = connectedClient
  32.  
  33.     insertSampleDocuments((err) => {
  34.         if (err) {
  35.             console.log('Error inserting sample documents: ' + err)
  36.             process.exit(2)
  37.         }
  38.         app.listen(3000, function() {
  39.         console.log('Listening on port 3000...')
  40.     })
  41.     }
  42. })
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. var insertSampleDocuments = function(db, callback) {
  50.     db.collection('families').insertOne( {
  51.         "id": "PetersenFamily",
  52.         "lastName": "Petersen",
  53.         "parents": [
  54.             { "firstName": "Thomas" },
  55.             { "firstName": "Mary Kay" }
  56.         ],
  57.         "children": [
  58.             { "firstName": "John", "gender": "male", "grade": 7 }
  59.         ],
  60.         "pets": [
  61.             { "givenName": "Fluffy" }
  62.         ],
  63.         "address": { "country": "USA", "state": "WA", "city": "Seattle" }
  64.     }, callback);
  65. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top