Mongoskin is used in node. js to manipulate the mongoDB instance
- 2020-03-30 04:00:56
- OfStack
A nonsense,
In January 2013, I contacted with mongodb for development, and developed tourism tag service, weibo tag retrieval system, map service, and web APP service. Scenarios using MongoDB moved from.net and JAVA environments to the node.js platform. The more you find that node. js and mongodb work together, the better it feels. It feels like mongodb and node.js are a natural match. Indeed, the mongodb client is the JS parsing engine. Therefore, choosing mongodb and node.js as prototypes is also a nice choice. Online, met a netizen asked the development of mongodb, which driver is the best choice, before has been using the native driver, but there are a lot of code to pay attention to, such as the connection of the close operation and so on... Therefore, I recommend mongoskin in the node.js development environment.
Two, a few need to say the concept
(1) database: same as relational database.
(2) collection: a table in a relational database.
(3) documents: analogous to the records of a relational database, they are actually JSON objects.
(4) database design: it is recommended to consider the design of NoSQL and discard the design idea of relational data; In fact, NoSQL database design is extensive and profound, and needs to be constantly practiced in the project.
(5) user system: each database has its own administrator, which can:
use dbname; db.addUser('root_1' , 'test');
(7) it is recommended to change the external port
(8) start the service (this is under win, slightly modified under Linux) :
mongod --dbpath "XXMongoDBdatadb" --logpath "XXMongoDBlogmongo.log" --logappend -auth --port 7868
Iii. Set up mongodb development infrastructure
(0) NPM install mongoskin
Node.js installation, package, and other mechanisms are not covered here.
(1) create config file config.json
{
"dbname":"TEST",
"port": "7868",
"host": "127.0.0.1",
"username": "test",
"password": "test"
}
(2) create util related class mongo.js: export a DB object
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 base classes for CRUD: to reduce the repetition of the CURD code, just pass in the relevant JSON objects
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: The inserted record, JSON The format of model
* @callback : callback to return the insert success 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 : reads a record
* @query : query conditions, Mongo Of the query JSON literal
* @callback : callback to return the required record 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, Mongo Of the query JSON The literal here is _id
* @updateModel : need to be updated JSON Format model
* @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 : deletes a record
* @query : query conditions, Mongo Of the query JSON literal
* @callback : returns a failure or success message
*
* */
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 states are needed to represent success and failure, later it can be expanded into verification code error, SMS verification error, user name error, etc
module.exports = {
/*
* The successful state
*
* */
success: {
status: 1,
message: 'OK'
},
/*
* The failure state
*
* */
fail: {
status: 0,
message: 'FAIL'
},
/*
* The passwords entered are inconsistent
* */
repeatPassword: {
status: 0,
message: ' The passwords entered are inconsistent '
}
};