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!'});
            }
        }
    });
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值