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.