|
@@ -5,6 +5,8 @@
|
|
|
* @date 2017/5/22.
|
|
|
*/
|
|
|
|
|
|
+import mongoose from "mongoose";
|
|
|
+
|
|
|
class MongooseHelper {
|
|
|
|
|
|
/**
|
|
@@ -31,6 +33,7 @@ class MongooseHelper {
|
|
|
findOne(conditions, fields = null) {
|
|
|
let self = this;
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
+ conditions = self._convertId(conditions);
|
|
|
self.model.findOne(conditions, fields, function (error, data) {
|
|
|
if (error) {
|
|
|
reject(null);
|
|
@@ -177,6 +180,7 @@ class MongooseHelper {
|
|
|
update(condition, updateData) {
|
|
|
let self = this;
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
+ condition = self._convertId(condition);
|
|
|
self.model.update(condition, {$set: updateData}, function(error, data) {
|
|
|
if (error) {
|
|
|
reject(error);
|
|
@@ -187,6 +191,23 @@ class MongooseHelper {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * id转换为objectId
|
|
|
+ *
|
|
|
+ * @param {object} condition
|
|
|
+ * @return {object}
|
|
|
+ */
|
|
|
+ _convertId(condition) {
|
|
|
+ // 对于ID的处理
|
|
|
+ if (condition === null || condition._id === undefined) {
|
|
|
+ return condition;
|
|
|
+ }
|
|
|
+ let result = mongoose.Types.ObjectId(condition._id);
|
|
|
+ condition._id = result;
|
|
|
+
|
|
|
+ return condition;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
export default MongooseHelper;
|