예제 #1
0
파일: mongo.py 프로젝트: uchuugaka/tbag
 async def find_one_and_update(self,
                               spec,
                               update_fields,
                               upsert=False,
                               return_document=False,
                               fields=None,
                               cursor=None):
     """ 查询一条指定数据,并修改这条数据
     @param spec 查询条件
     @param update_fields 更新字段
     @param upsert 如果不满足条件,是否插入新数据,默认False
     @param return_document 返回修改之前数据或修改之后数据,默认False为修改之前数据
     @param fields 需要返回的字段,默认None为返回全部数据
     @return result 修改之前或之后的数据
     @param cursor 查询游标,如不指定默认使用self.cursor
     """
     if not cursor:
         cursor = self.cursor
     spec[DELETE_FLAG] = {'$ne': True}
     if '_id' in spec:
         spec['_id'] = self._convert_id_object(spec['_id'])
     set_fields = update_fields.get('$set', {})
     set_fields['update_time'] = tools.get_utc_time()
     update_fields['$set'] = set_fields
     result = await cursor.find_one_and_update(
         spec,
         update_fields,
         projection=fields,
         upsert=upsert,
         return_document=return_document)
     if result and '_id' in result:
         result['_id'] = str(result['_id'])
     return result
예제 #2
0
파일: mongo.py 프로젝트: uchuugaka/tbag
 async def insert(self, docs_data, cursor=None):
     """ 插入数据
     @param docs_data 插入数据 dict或list
     @param ret_ids 插入数据的id列表
     @param cursor 查询游标,如不指定默认使用self.cursor
     """
     if not cursor:
         cursor = self.cursor
     docs = copy.deepcopy(docs_data)
     ret_ids = []
     is_one = False
     create_time = tools.get_utc_time()
     if not isinstance(docs, list):
         docs = [docs]
         is_one = True
     for doc in docs:
         doc['_id'] = ObjectId()
         doc['create_time'] = create_time
         doc['update_time'] = create_time
         ret_ids.append(str(doc['_id']))
     cursor.insert_many(docs)
     if is_one:
         return ret_ids[0]
     else:
         return ret_ids
예제 #3
0
파일: mongo.py 프로젝트: uchuugaka/tbag
 async def update(self,
                  spec,
                  update_fields,
                  upsert=False,
                  multi=False,
                  cursor=None):
     """ 更新
     @param spec 更新条件
     @param update_fields 更新字段
     @param upsert 如果不满足条件,是否插入新数据
     @param multi 是否批量更新
     @return modified_count 更新数据条数
     @param cursor 查询游标,如不指定默认使用self.cursor
     """
     if not cursor:
         cursor = self.cursor
     spec[DELETE_FLAG] = {'$ne': True}
     if '_id' in spec:
         spec['_id'] = self._convert_id_object(spec['_id'])
     set_fields = update_fields.get('$set', {})
     set_fields['update_time'] = tools.get_utc_time()
     update_fields['$set'] = set_fields
     if not multi:
         result = await cursor.update_one(spec,
                                          update_fields,
                                          upsert=upsert)
         return result.modified_count
     else:
         result = await cursor.update_many(spec,
                                           update_fields,
                                           upsert=upsert)
         return result.modified_count
예제 #4
0
파일: mongo.py 프로젝트: xlelou/fp-server
 async def insert(self, docs_data):
     """ 插入数据
     @param docs_data 插入数据 dict或list
     @param ret_ids 插入数据的id列表
     """
     docs = copy.deepcopy(docs_data)
     ret_ids = []
     is_one = False
     create_time = tools.get_utc_time()
     if not isinstance(docs, list):
         docs = [docs]
         is_one = True
     for doc in docs:
         doc['_id'] = ObjectId()
         doc['create_time'] = create_time
         doc['modify_time'] = create_time
         ret_ids.append(str(doc['_id']))
     self.dao.insert_many(docs)
     if is_one:
         return ret_ids[0]
     else:
         return ret_ids