Coder Social home page Coder Social logo

mongo-cli's Introduction

Build a CLI Mongo App w/ Node

Note This exercise assumes a restaurant_db was created in the mongo intro lesson.

Your task is to connect to the restaurant_db via the mongodb node module and build a little CLI app that will allow a user to enter commands to query our database.

Setup:

Start by:

  1. forking and cloning this exercise
  2. creating a main js file, and installing the mongodb node module:
$ touch app.js
$ npm init
$ npm install --save mongodb

Great, now use an instance of the MongoClient to connect to the restaurant_db and search for all restaurants:

// app.js

var mongo = require("mongodb").MongoClient
var url = "mongodb://localhost:27017/restaurant_db"

mongo.connect(url, function(err, db){
  var collection = db.collection('restaurants');
  collection.find().toArray(function(err, docs){
    console.log(docs)
  })
})

If you see all the documents outputed to your server logs, you're in great shape!

If not, double check your connection url and that your database is in fact there and populated!

Now, add in the functionality to prompt the user for some input. To do that, we are going to use a node module that will allow us to use the prompt method, much like how we used it w/ front-end JS in the browser:

$ npm install --save prompt-sync

Now let's create a little menu prompt that will return all documents in our collection if the user enters in the right input:

// app.js

var mongo = require("mongodb").MongoClient
var url = "mongodb://localhost:27017/test"

mongo.connect(url, function(err, db){
  var collection = db.collection('restaurants');
  var number = prompt("Type 1 and press enter to display all restaurants' names: ")
  if(number == "1"){
    collection.find().toArray(function(err, doc){
      console.log(doc)
    })
  }
})

You do:

Add another prompt to let the user view more information about a restaurant.

Bonus!

Allow users to add their own restaurants

Double Bonus!

Allow users to edit/delete restaurants

mongo-cli's People

Contributors

tylercrosse avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.