def updateWithId(self, oId, obj, transaction=None): if "$set" not in obj: obj['$set'] = {} obj['$set']['updateAt'] = datetime.now() result = self.db.updateOne(self.coll, {"_id": oId}, obj, transaction) if result: CacheHelper.deleteCacheByOid(self.coll, oId) return result
def delete(self, oId=None, query=None): ### modify by fangming.fm if query == None and oId == None: return False if oId != None: self.db.remove(self.coll, {"_id": oId}) CacheHelper.deleteCacheByOid(self.coll, oId) else: self.db.remove(self.coll, query)
def get(self, oid, keys=None): obj = CacheHelper.getCacheByOid(self.coll, oid) if obj: return json.loads(obj) else: obj = self.db.find_one(self.coll, {"_id": oid}, keys) if obj: CacheHelper.setCacheByOid(self.coll, oid, json.dumps(obj, cls=MeEncoder)) return obj
def updateOne(self, query, obj, transactions=None, upsert=False): if "$set" not in obj: obj['$set'] = {} if not obj['$set'].has_key("updateAt"): obj['$set']['updateAt'] = datetime.now() result = self.db.updateOne(self.coll, query, obj, transactions, upsert) if result: CacheHelper.setCacheByOid(self.coll, result['_id'], json.dumps(result, cls=MeEncoder)) return result
def update(self, oId, obj, transaction=None, updateAt=True): log.warn("update is deprecated, please use updateWithId!") if "$set" not in obj: obj['$set'] = {} if updateAt: obj['$set']['updateAt'] = datetime.now() result = self.db.updateOne(self.coll, {"_id": oId}, obj, transaction) if result: CacheHelper.deleteCacheByOid(self.coll, oId) return result
def update_core(self, oId, obj): log.debug('[%s/%s] update object start: %s' % (self.appName, self.className, obj)) # 用于保存递归对象,递归对象内容不存到父对象 embed = None if obj.has_key('$set'): for key in obj['$set'].keys(): # 如果是pointer类型,则递归处理对象 if isinstance( obj['$set'][key], dict) and obj['$set'][key].has_key('_type') and obj[ '$set'][key]['_type'] == 'pointer': # 创建对应的classhelper c = ClassHelper(obj['$set'][key]['_class'], self.appName) embed = {} embed[key] = deepcopy(obj['$set'][key]) # 如果存在_id,则递归update if obj['$set'][key].has_key('_id'): o = c.update_core(obj['$set'][key]['_id'], obj['$set'][key]['_content']) else: o = c.create_core(obj['$set'][key]['_content']) # 删除_content,使得父对象存储pointer对象 ''' TODO 此处无法保存updateAt和createAt返回客户端 ''' del obj['$set'][key]['_content'] obj['$set'][key]['_id'] = o[0]['_id'] print "Key:" + key print "O: " + str(o) print "embed1: " + str(embed) embed[key]['_id'] = o[0]['_id'] # 将递归对象拼接起来 if o[1] != None: if embed[key]['_content'].has_key('$set'): del embed[key]['_content']['$set'] # 内部还有递归的情况,同级可能有多个递归对象c for embed_key in o[1].keys(): embed[key]['_content'][embed_key] = o[1][embed_key] else: embed[key]['_content'] = o[0] print "embed2: " + str(embed) else: obj['$set'] = {} obj['$set']['updateAt'] = datetime.now() try: result = (self.db.update(self.coll, {"_id": oId}, obj), embed) if result: CacheHelper.deleteCacheByOid(self.coll, oId) return result except Exception, e: return (ERR_OBJECTID_MIS.message, {})
def find(self, query, keys=None, sort=None, limit=0, skip=0, cache_next_page=False): if ClassHelper.is_online: result = [] condition = [ query, ] if keys: condition.append(keys) if sort: condition.append(sort) if limit != 0: condition.append(limit) objids = CacheHelper.getCacheByCondition(self.coll, condition) if objids: for objid in objids: obj = self.get(objid, keys) if obj: result.append(obj) return result else: tmpkeys = copy.deepcopy(keys) if tmpkeys and tmpkeys.has_key('_id') and tmpkeys['_id'] == 0: del tmpkeys['_id'] if tmpkeys == {}: tmpkeys = None cursor = self.db.cursor(self.coll, query, tmpkeys, sort, limit, skip) if cursor: objids = [] for obj in cursor: objids.append(MongoDb.toId(obj['_id'])) self.get(MongoDb.toId(obj['_id']), keys) if keys and keys.has_key('_id') and keys['_id'] == 0: del obj['_id'] CacheHelper.setCacheByCondition(self.coll, condition, objids) self.cursor = copy.deepcopy(cursor) if cache_next_page and objids != []: if sort and sort.has_key('_id'): if sort['_id'] == 1: query["_id"] = {"$gt": ObjectId(objids[-1])} elif sort['_id'] == -1: query["_id"] = {"$lt": ObjectId(objids[-1])} elif query.has_key('_id'): if query['_id'].has_key('$gt'): query["_id"] = {"$gt": ObjectId(objids[-1])} elif query['_id'].has_key('$lt'): query["_id"] = {"$lt": ObjectId(objids[-1])} self.find(query, keys, sort, limit, cache_next_page=False) else: self.cursor = self.db.cursor(self.coll, query, keys, sort, limit, skip) return self