1. 程式人生 > >nodejs mongoose 操作mongodb 資料庫封裝

nodejs mongoose 操作mongodb 資料庫封裝

/**
 * 公共Add方法
 * @param model 要操作資料庫的模型
 * @param conditions 增加的條件,如{id:xxx}
 * @param callback 回撥方法
 */
exports.addData = function (model, conditions, callback) {

    model.create(conditions, function (err, result) {
        if (err) {
            console.log(err);
            callback({success: 0, flag: "save data fail"
}); } else { console.log('save success'); callback({success: 1, flag: "save data success"}); } }); } /** * 公共update方法 * @param model 要操作資料庫的模型 * @param conditions 增加的條件,如{id:xxx} * @param update 更新條件{set{id:xxx}} * @param options * @param callback */
exports.updateData = function (model, conditions, update, options, callback) { model.update(conditions, update, options, function (error, result) { if (error) { console.log(error); callback({success: 0, flag: "update data fail"}); } else { if (result.n != 0
) { console.log('update success!'); callback({success: 1, flag: "update data success"}); } else { console.log('update fail:no this data!'); callback({success: 0, flag: 'update fail:no this data!'}); } } }); } /** * 公共remove方法 * @param model * @param conditions * @param callback */ exports.removeData = function (model, conditions, callback) { model.remove(conditions, function (error, result) { if (error) { console.log(error); callback({success: 0, flag: "remove data fail"}); } else { if (result.result.n != 0) { console.log('remove success!'); callback({success: 1, flag: "remove data success"}); } else { console.log('remove fail:no this data!'); callback({success: 0, flag: 'remove fail:no this data!'}); } } }); } /** * 公共find方法 非關聯查詢 * @param model * @param conditions * @param fields 查詢時限定的條件,如順序,某些欄位不查詢等 * @param options * @param callback */ exports.findData = function (model, conditions, fields, options, callback) { var sort = options.sort == undefined ? {_id:-1} : options.sort; delete options.sort; model.find(conditions, fields, options, function (error, result) { if (error) { console.log(error); callback({success: 0, flag: "find data fail"}); } else { if (result.length != 0) { console.log('find success!'); callback({success: 1, flag: "find data success", result: result}); } else { console.log('find fail:no this data!'); callback({success: 0, flag: 'find fail:no this data!'}); } } }).sort(sort); } /** * 公共populate find方法 * 是關聯查詢 * @param model * @param conditions * @param path :The field need to be refilled (需要覆蓋的欄位) * @param fields :select fields (name -_id,Separated by a space field,In front of the field name plus "-"said not filled in) * @param refmodel (關聯的欄位,有path可為null) * @param options * @param callback */ exports.findDataPopulation = function (model, conditions, path, fields, refmodel, options, callback) { model.find(conditions).populate(path, fields, refmodel, options).exec(function (err, result) { if (err) { console.log(err); callback({success: 0, flag: 'population find data fail'}); } else { if (result.length != 0) { console.log('population find success!'); callback({success: 1, flag: 'population find data success', result: result}); } else { console.log('population find fail:no this data!'); callback({success: 0, flag: 'population find fail:no this data!'}); } } }); }