node-mongodb增删改查示例
1.1:下载mongodb模块
2.1:创建连接:
2.1.1:
引入模块
var mongoose = require('mongoose');
创建连接池:
第一种方法:
var config = require('../../../../config');
var mongodb = mongoose.connect(config.mongodb);
第二种方法 基于express框架方法:
var mongoUtils = require('gmdp').init_gmdp.core_mongoose_utils;
var mongoose = mongoUtils.init();
mongoose.set("debug",true);(错误机制)
3.1: 创建模式:
var Schema = mongoose.Schema;
var purchaseResultSchema = new Schema(
{
需要使用的字段
},{
collection:"集合"
}
exports.$PurchaseResult = mongoose.model("向外暴露名",purchaseResultSchema );
4.1: 增删改查:
4.1.1:引入model
var user_model = require('gmdp/lib/common/core/models/user_model');
var processRoleModel = require("../../../common/processRole/models/processRoleModel");
4.1.2:增加-->$PurchaseResult(param).save();
exports.addPurchaseResult = function(param,cb){
result_model.$PurchaseResult(param).save(function(error,result){
cb(error,result)
});
};
4.1.3: 修改-->.update({修改条件},{修改值},function(err, result)
exports.updatePurchaseResult = function(id,entity,cb){
var update = {$set: entity};
var options = {};
result_model.$PurchaseResult.update({_id:id},update,options,function (error,result) {
cb(error,result)
})
};
4.1.4: 删出-->.remove({删出条件},function(err, result);
exports.deleteBidResult = function(param,cb){
result_model.$BidResult.remove(param,function(error){
cb(error);
});
};
4.1.5: 查询-->.find({查询条件},function(err, result){});