mongodb

MongoDB - document based, NoSQL database.

There are 4 snippets with the mongodb tag

  • Getting Started with MongoDB and mongojs

    How to get started with MongoDB under node.js, using the mongojs module.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      db.products.find(function(err, products) {
        if(err) { throw err; }
    
        res.writeHead(200, {"Content-Type": "text/html"});
        res.write("<table>");
    
        products.forEach(function(product) {
          res.write("<tr>");
          res.write("<td>" + product.make + "</td>");
          res.write("<td>" + product.model + "</td>");
          res.write("</tr>");
        });
    
        res.end("</table>");
      });
    
    }).listen(8080);
    1 Sep 2013
  • Insert a Document into MongoDB using node.js and mongojs

    Insert a document into a MongoDB database using the mongojs module under node.js.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      var data = {
        make: "Nokia",
        model: "Lumia"
      };
    
      db.products.insert(data, function(err, result) {
        if(err) { throw err; }
        res.write("<p>Product inserted:</p>");
        res.end("<p>" + result[0].make + " " + result[0].model + "</p>");
      });
    
    }).listen(8080);
    
    1 Sep 2013
  • Remove a MongoDB document using node.js and mongojs

    Remove (delete) a document from a MongoDB database using the mongojs module and node.js.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      var query = {
        // WHERE make = 'Samsung'
        make: "Samsung"
      };
    
      db.products.remove(query, function(err, result) {
        if(err) { throw err; }
        res.end("<p>Product removed");
      });
    
    }).listen(8080);
    1 Sep 2013
  • Update a MongoDB document using node.js and mongojs

    Update a document in a MongoDB database using the mongojs module and node.js.

    var http = require("http");
    var mongo = require("mongojs");
    
    var db = mongo.connect("shop", ["products"]);
    
    http.createServer(function(req, res) {
    
      var update = {
        // SET model = 'iPad 3' 
        $set: { model: "iPad 3"}
      };
    
      var query = {
        // WHERE model = 'iPad' 
        model: "iPad"
      };
    
      db.products.update(query, update, function(err, result) {
        if(err) { throw err; }
        res.end("<p>Product updated</p>");
      });
    
    }).listen(8080);
    
    1 Sep 2013