A. MongoDB:
Because 10gen is one of the sponsors, MongoDB has good node. JS support.
A. Basic support: There are two commonly used components in node. JS support for MongoDB MongoDB, Mongoose. The following are described separately.
(1) MongoDB-based support. The driver for node. JS is event-driven, so the usage is basically asynchronous callback functions. Download driver Components $npm INSTALL-GD MongoDB
Add the following code to Testdb.js:
var MongoDB = require (' MongoDB ');
var server = new MongoDB. Server ("127.0.0.1", 27017, {});
var connection = new MongoDB. Db (' Test ', server, {safe:true}). Open (Function (error, client) {
if (error) throw error;
var collection = client.collection (' users ');
var test =collection.find ({}, {limit:10}). ToArray (function (err, docs) {
Console.dir (Docs);
});
Insert Section
var Doc1 = {id:33,name: ' liuyang1 '};
var doc2 = {id:44,name: ' liuyang2 '};
Collection.insert ([Doc1,doc2],function (Err,result) {
Console.log (result);
});
Console.log (' This is a Test result......\n ');
});
(2) Support based on Mongoose.
Download driver components $npm INSTALL-GD Mongoose
The test code is as follows:
var mongoose = require (' Mongoose ');
var conn = mongoose.connect (' mongodb://localhost/test ');
var db = mongoose.connection;
Db.on (' Error ', Console.error.bind (console, ' connection error: '));
Db.once (' open ', function callback (ERR) {
if (!err) {console.log (' opened ');}
});
var Schema = Mongoose. Schema;
var person = new Schema ({
Id:number
, name: {type:string}
});
var result = Mongoose.model (' users ', person);
var personcollection = Mongoose.model (' users ');
var one = new Personcollection ();
One.title = "Zyq";
One.age = 38;
One.save (function (err) {
if (err) {
Console.log (' failed!!!! ');
}else{
Console.log (' successful!!!! ');
}//End of If block
});
Test find
Personcollection.find (function (err, persons) {
if (!err) {
Console.log (persons);
}
});
Rest support for B.mongodb:
Due to the Bjson data storage mode of MongoDB, it is natural to support JSON format data. Rest support is most convenient to create under some conditions.
1.-rest startup parameters support for Web page management:
2. Fast build based on node. JS, support RESTful WEB service mode, JSON format MONGODB Access platform:
First install the MongoDB driver: $npm install MONGODB-GD.
Installing MongoDB's Rest components: $npm Install MONGODB-REST-GD
Run Mongodb-rest directly. (Before running, locate this component directory (in the Nodejs Global module directory such as:/usr/local/nodejs/lib/node_modules/mongodb-rest, Find Config.json. Modify the server address port of MongoDB to the correct data, or the unauthorized error will be reported)
If said can not run, and reported that createserver error, it is necessary to modify the component directory of Server.js to the var app = Module.exports.app = Express.createserver (), to var app = Module.exports.app = Express ();
The code is a bit old: but it's good to study.
MongoDB can now be accessed via rest Web service, and we can test it with curl:
$$ curl-d ' {"A1": 201} '-h ' Content-tyson ' http://localhost:3000/test/example
Two, Redis database:
The advantages and disadvantages of Redis and the application scenario is not mentioned here, you can find my other logs to understand. If used in a more complex cache scenario, it's true, at least from the current technical point of view.
1. Install the Redis database. Download the installation package to the official website.
http://redis.io/here should be 2.6.10 version.
$tar-XZVF redis-2.6.10.tar.gz
$CD redis-2.6.10
$make
$make Install
OK now you can test it with Redis's own client.
Run Redis server First
$redis-server
$redis-CLI
>redis 127.0.0.1:6379> Set Zyq ' is a good mans '
Redis 127.0.0.1:6379>get Zyq
Show ' is a good man ' OK, everything works
To install the Redis driver module for node. JS:
$sudo NPM Install REDIS-GD
After the installation, you can write code to test:
var Redis = require ("Redis");
var client = Redis.createclient ();
Client.on ("Error", function (Err) {Console.log ("error" + Err);});
Client.set ("Skey", "This is a string", redis.print);
Client.hset ("HashKey", "Hashtest a", "abc", Redis.print);
Client.hset (["HashKey", "Hashtest B", "some Otest"], redis.print);
Client.hkeys ("HashKey", function (err, replies) {
Console.log (Replies.length + "replies:");
Replies.foreach (function (reply, i) {
Console.log ("" + i + ":" + Reply);
});
Client.quit ();
});
Third, MySQL database
Four, HBase database:
---to be continued-----
Category: Databases, Web development _nodejs Tags: NOSQL, MongoDB, Redis, node. js, MySQL http://www.cnblogs.com/windwithlife/archive/2013/02/25/ 2923235.html
Reproduced Database Redis,mongodb,hbase,mysql on the node. JS Platform