Nodejs Connection MongoDB database implementation and deletion and change check _node.js

Source: Internet
Author: User
Tags eval install mongodb mongoclient mongodb

Get ready

1. Install MongoDB via NPM command

2. Installation of MongoDB database, here is not described in detail, installation URL: http://www.jb51.net/article/82522.htm

CRUD Operations

Prior to this should be aware of the MongoDB database, know some of its additions and deletions to change the command.

1. Increase

var mongoclient = require ("MongoDB"). mongoclient;
var db_url = "Mongodb://localhost:27017/chm";

function InsertData (db)
{
  var devices = db.collection (' VIP ');
  var data = {"Name": "Node", "Age": "addr": "NB", "Addtime": New Date ()};
  Devices.insert (data,function (Error, result) {
    if (error)
    {
      console.log (' ERROR: ' + error);
    } else{

      Console.log (RESULT.RESULT.N);
    }
    Db.close ();
  });

 
Mongoclient.connect (Db_url, function (Error, DB) {
  console.log (' Connection succeeded! ');
  InsertData (db);
});

2. Find

var MongoDB = require (' MongoDB ')
var mongoclient = require (' MongoDB '). mongoclient;
var db_conn_str = ' Mongodb://localhost:27017/chm '; 

var selectdata = function (db, callback) { 
 //connection to table 
 var collection = db.collection (' VIP ');
 Query data
 var wherestr = {"Name": ' node '};
 Collection.find (wherestr,function (error, cursor) {
  Cursor.each (function (error,doc) {
    if (doc) {
      // Console.log (DOC);
      if (doc.addtime) {
        console.log ("Addtime:" +doc.addtime);

}});} Mongoclient.connect (DB_CONN_STR, function (err, DB) {
 Console.log ("Successful connection!") ");
 Selectdata (DB, function (result) {
  console.log (result);
  Db.close ();
 });


3. Update

var mongoclient = require ("MongoDB"). mongoclient;
var db_url = "Mongodb://localhost:27017/chm";

Mongoclient.connect (Db_url, function (Error, DB) {
  Console.log ("Connected successfully!");
  UpdateData (db);
});

function UpdateData (db)
{
  var devices = db.collection (' VIP ');
  var wheredata = {"Name": "Node"}
  var updatedat = {$set: {"Age": 26}};//If you do not use $set, replace the entire data
  devices.update ( Wheredata, Updatedat, function (error, result) {
    if (error) {
      console.log (' ERROR: ' + error);
    } else{
      Console.log (result);
    }
    Db.close ();
  });


4. Delete

var mongoclient = require (' MongoDB '). mongoclient;
var db_url = "Mongodb://localhost:27017/chm";

Mongoclient.connect (Db_url, function (Error, DB) {
  Console.log ("successful Connection");
  DeleteData (db);
});

function DeleteData (db)
{
  var devices = db.collection (' VIP ');
  var data = {"Name": "Node"};
  Devices.remove (data, function (error, result) {
    if (error) {
      console.log (' ERROR: ' + error);
    } else{
      Console.log (RESULT.RESULT.N);
    }
    Db.close ();
  })
}

Stored Procedures

Creating a stored procedure in MongoDB

All stored procedures are stored in Db.system.js, via db.eval ("Stored procedure ID ()"), and call stored procedures.

Called in code:

var mongoclient = require ("MongoDB"). mongoclient;
var db_url = "Mongodb://localhost:27017/chm";

Mongoclient.connect (Db_url, function (error,db) {
  console.log ("Connection succeeded!");
  Callprocess (db)
});

function callprocess (db)
{
  db.eval ("Get_vip_count ()", function (error, result) {
    if (error)
      { Console.log (Error);
    } else{
      Console.log ("Count:" +result);
    }
    Db.close ();
  });


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.