1. Nonsense
Since I came into contact with mongodb in January 2013, I have developed travel tag services, Weibo tag retrieval system, map services, and web APP services... The scenarios using MongoDB have been transferred from the .NET and JAVA environment to the node.js platform. I feel more and more that Node.js and mongodb are combined with each other very well. It feels like mongodb and node.js are born with a pair. Indeed, mongodb's client is JS's parsing engine. Therefore, choosing mongodb and node.js as product prototypes is also a very nice choice. On the Internet, I encountered netizens asking about the development of mongodb, which driver is the best choice. I used to use native drivers, but there are many things to pay attention to when writing code, such as the connection closing operation, etc. Therefore, in the node.js development environment, I recommend using mongoskin here.
2. Several concepts that need to be discussed
(1) Database: the same as relational database.
(2) Collection: Tables in relational database.
(3) Document: A record similar to the relational database is actually a JSON object.
(4) Database design: It is recommended to consider NoSQL design and abandon the design idea of relational data; in fact, NoSQL database design is profound and needs to be continuously practiced in the project.
(5) User system: Each database has its own administrator, which can:
The code copy is as follows:
use dbname; db.addUser('root_1' , 'test');
(7) It is recommended to change the external port
(8) Start the service (this is a little modified under win and linux):
The code copy is as follows:
mongod --dbpath "XX/MongoDB/data/db" --logpath "XX/MongoDB/log/mongo.log" --logappend -auth --port 7868
3. Build mongodb development infrastructure
(0) npm install mongoskin install mongoskin
Node.js installation, package and other mechanisms are not introduced here.
(1) Create configuration file config.json
The code copy is as follows:
{
"dbname":"TEST",
"port": "7868",
"host": "127.0.0.1",
"username": "test",
"password": "test"
}
(2) Create util-related class mongo.js: Export a DB object
The code copy is as follows:
var mongoskin = require('mongoskin'),
config = require('./../config.json');
/*
* @des: Export the database connection module
* */
module.exports = (function(){
var host = config.host,
port = config.port,
dbName = config.dbname,
userName = config.username,
password = config.password,
str = 'mongodb://' + userName + ':' + password + '@' + host +':' + port+ '/' + dbName;
var option = {
native_parser: true
};
return mongoskin.db(str, option);
})();
(3) Build the basic class of CRUD: In order to reduce duplicate CURD code, you only need to pass in the relevant JSON object.
The code copy is as follows:
var db = require('./mongo.js'),
status = require('./status'),
mongoskin = require('mongoskin');
var CRUD = function(collection){
this.collection = collection;
db.bind(this.collection);
};
CRUD.prototype = {
/*
* @des: Create a record
* @model: Inserted record, model in JSON format
* @callback: callback, return the record of successful insertion or failure information
*
* */
create: function(model, callback){
db[this.collection].save(model, function(err, item){
if(err) {
return callback(status.fail);
}
item.status = status.success.status;
item.message = status.success.message;
return callback(item);
});
},
/*
* @des: Read a record
* @query: query conditions, JSON literal of Mongo query
* @callback: Callback, return records that meet the requirements or failure information
*
* */
read: function(query, callback){
db[this.collection].find(query).toArray(function(err, items){
if(err){
return callback(status.fail);
}
var obj = {
status: status.success.status,
message: status.success.message,
items: items
};
return callback(obj);
});
},
/*
* @des: Update a record
* @query: query conditions, JSON literal of Mongo query, here is _id
* @updateModel: Model in JSON format that needs to be updated
* @callback: Returns success or failure information
*
* */
update: function(query, updateModel, callback){
var set = {set: updateModel};
db[this.collection].update(query, set, function(err){
if(err){
return callback(status.fail);
}else{
return callback(status.success);
}
});
},
/*
* @des: Delete a record
* @query: query conditions, JSON literal of Mongo query
* @callback: Returns the message of failure or success
*
* */
deleteData: function(query, callback){
db[this.collection].remove(query, function(err){
if(err){
return callback(status.fail);
}
return callback(status.success);
});
}
};
module.exports = CRUD;
(4) Build status.json, because some statuses are required to indicate success and failure, it can be expanded to verification code errors, SMS verification errors, username errors, etc. in the later stage.
The code copy is as follows:
module.exports = {
/*
* Success status
*
* */
success: {
status: 1,
message: 'OK'
},
/*
* Failed status
*
* */
Fail: {
status: 0,
message: 'FAIL'
},
/*
* The password entered twice is inconsistent
* */
repeatPassword: {
status: 0,
message: 'The passwords entered twice are inconsistent'
}
};