node-mongodb增刪改查示例

1.1:下載mongodb模塊

2.1:創(chuàng)建連接:

2.1.1:

引入模塊

var mongoose = require('mongoose');

? ? 創(chuàng)建連接池:

第一種方法:

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: 創(chuàng)建模式:

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){});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容