Example #1
0
    def deldoc(self):
        """Deletes specified document in the database.
        """
        mode, kwargs = self.mode, self.kwargs
        dbname = dbu.get_dbname(**kwargs)
        client = self.client()
        if not self.check_database(client, dbname): return

        detname = kwargs.get('detector', None)
        if detname is None:
            logger.warning(
                '%s needs in the collection name. Please specify the detector name.'
                % (mode))
        colname = detname
        db, fs = dbu.db_and_fs(client, dbname)
        colnames = dbu.collection_names(db)

        if not (colname in colnames):  # dbu.collection_exists(db, colname)
            logger.warning('db "%s" does not have collection "%s"' %
                           (db.name, str(colname)))
            return

        col = dbu.collection(db, colname)

        logger.info('command mode: "%s" db: "%s" collection: "%s"' %
                    (mode, db.name, str(colname)))

        defs = self.defs
        ctype = kwargs.get('ctype', None)
        run = kwargs.get('run', None)
        tsec = kwargs.get('time_sec', None)
        tstamp = kwargs.get('time_stamp', None)
        vers = kwargs.get('version', None)
        confirm = kwargs.get('confirm', False)

        query = {'detector': detname}
        if ctype != defs['ctype']: query['ctype'] = ctype
        if run != defs['run']: query['run'] = run
        if vers != defs['version']: query['version'] = vers
        #if tsec  != defs['time_sec'] : query['time_sec'] = tsec
        if is_in_command_line('-s', '--time_sec'): query['time_sec'] = tsec
        if is_in_command_line('-t', '--time_stamp'):
            query['time_stamp'] = tstamp

        logger.info('query: %s' % str(query))

        docs = dbu.find_docs(col, query)
        if docs is None or docs.count() == 0:
            logger.warning('Can not find document for query: %s' % str(query))
            return

        for i, doc in enumerate(docs):
            msg = '  deldoc %2d:'%i + doc['time_stamp'] + ' ' + str(doc['time_sec'])\
                + ' %s'%doc['ctype'].ljust(16) + ' %4d'%doc['run'] + ' ' + str(doc['id_data'])
            logger.info(msg)
            if confirm:
                dbu.delete_document_from_collection(col, doc['_id'])
                dbu.del_document_data(doc, fs)

        if not confirm: dbu.request_confirmation()
Example #2
0
def delete_documents(dbname, colname, doc_ids):
    """Delete documents with _id-s in doc_ids from dbname, colname
    """
    #logger.debug('Deleting documents:\n  %s' % ('\n  '.join(doc_ids)))
    client = connect_client()
    db, fs = dbu.db_and_fs(client, dbname)
    col = collection(db, colname)
    #msg = 'Deleted documents from db: %s col: %s' % (dbname, colname)
    for s in doc_ids:
        oid = ObjectId(s)
        doc = dbu.find_doc(col, query={'_id': oid})
        if doc is None: continue
        #msg += '\n  %s and its data' % doc.get('_id', 'N/A')
        dbu.del_document_data(doc, fs)
        dbu.delete_document_from_collection(col, oid)