Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoFrames doesn't delete document #13

Closed
njordr opened this issue Dec 11, 2016 · 11 comments
Closed

MongoFrames doesn't delete document #13

njordr opened this issue Dec 11, 2016 · 11 comments
Labels

Comments

@njordr
Copy link

njordr commented Dec 11, 2016

Hi.

A strange issue: the Frame class doesn't delete the document from the db. Insert and update works fine.

            str_id = '{}_{}_{}_{}_{}'.format(
                data_before.get('CodImpianto'),
                data_before.get('Armadio'),
                data_before.get('DateVar'),
                data_before.get('ShelfEvent'),
                data_before.get('Counter'),
            )
            log_id = sha224(str_id.encode()).hexdigest()
            previous_record = SDClosetLogs().by_id(log_id)

            if previous_record is not None:
                logger.debug('Deleting document {}.'.format(previous_record._id))
                try:
                    # previous_record.delete()
                    SDClosetLogs.delete(SDClosetLogs().by_id(log_id))
                except Exception as e:
                    logger.error('Cannot delete document {}. Error: {}'.format(previous_record._id, e))
                    return False

This is the log:
2016-12-11 16:30:36 DEBUG 3700 spy_replicator.etl_modules.etl_spydoc etl_spydoc.py:219 => Deleting document c4e599fb7467db9f868746631b7daea81a32fc63a2be05871b5710d8. (no exception logged)
I tried using both the previous_record instance and the general Frame class.

This is the documents in collections pre and after the delete operation:
image

This is the Frame class

class SDClosetLogs(Frame):
    _db = 'spy'
    _collection = 'logs'
    _fields = {
        '_id',
        'date',
        'type',
        'subtype',
        'installation',
        'closet',
        'shelf_event',
        'tag_id',
        'tag_user',
        'description',
        'counter'
    }
@njordr njordr changed the title MongoFrames do not delete document MongoFrames doesn't delete document Dec 11, 2016
@anthonyjb
Copy link
Member

Hi @njordr - we use the delete function quite a bit and clearly it passes the tests currently so my guess would be that this is specific to your scenario, like you highlight I'd typically expect this to be done like so:

SDClosetLogs().by_id(log_id).delete()

If this isn't working then it sounds like the something specific to the SDClosetLogs class set up is causing the issue, could you post the code for that class so I can review please?

@njordr
Copy link
Author

njordr commented Dec 11, 2016

This is my SDClosetLogs class

class SDClosetLogs(Frame):
    _db = 'spy'
    _collection = 'logs'
    _fields = {
        '_id',
        'date',
        'type',
        'subtype',
        'installation',
        'closet',
        'shelf_event',
        'tag_id',
        'tag_user',
        'description',
        'counter'
    }

@anthonyjb
Copy link
Member

@njordr - maybe it's an issue setting the _collection or _db, everything looks fine with it. I'll set it up on my local machine and attempt to add a record, find it by ID then delete it. I'll let you know how I get on, btw are you setting the ID (_id) manually or allowing mongo to provide one?

@njordr
Copy link
Author

njordr commented Dec 11, 2016

I set the _id field manually

@anthonyjb
Copy link
Member

OK I'll make sure I do also, are you storing an ObjectID as the ID or are just a hash string?

@njordr
Copy link
Author

njordr commented Dec 11, 2016

hash string

@anthonyjb
Copy link
Member

@njordr - So the following code sample works correctly for me, can you confirm it works for you or if it raises an error to help track the source of the issue?

from pymongo import MongoClient
from mongoframes import Frame


# Connect to database via MongoFrames
Frame._client = MongoClient(
    'mongodb://localhost:27017/njordr_test'
    )


# Define the docyment
class SDClosetLogs(Frame):
    _db = 'spy'
    _collection = 'logs'
    _fields = {
        '_id',
        'date',
        'type',
        'subtype',
        'installation',
        'closet',
        'shelf_event',
        'tag_id',
        'tag_user',
        'description',
        'counter'
    }


# Insert a document
document = SDClosetLogs(
    _id='c4e599fb7467db9f868746631b7daea81a32fc63a2be05871b5710d8',
    date='2016-12-11',
    type='foo',
    subtype='bar',
    installation=True,
    closet=False,
    shelf_event=True,
    tag_id='746631b7daea81a32fc63a2be05871b5710d8c4e599fb7467db9f868',
    tag_user='Ad8c4e599fb7467db9f746631b7daea81a32fc63a2be05871b5710d8',
    description='MongoFrames is a fast unobtrusive MongoDB ODM for Python \
        designed to fit into a workflow not dictate one.',
    counter=1
    )
document.insert()

# Check a document was inserted
print(SDClosetLogs.count())


# Find and delete the same document
document_ref = SDClosetLogs.by_id(document._id)
try:
    document_ref.delete()
except Exception as e:
    print(e)

# Check the document was deleted
print(SDClosetLogs.count()) 

@anthonyjb
Copy link
Member

The result should of course print out:

1
0

@njordr
Copy link
Author

njordr commented Dec 11, 2016

it works, I need to recheck carefully my code.

many thanks for your time

@anthonyjb
Copy link
Member

No problem, let me know how you get on and if I can help further :)

@njordr
Copy link
Author

njordr commented Dec 11, 2016

Stupid bug: I used the same code to update or delete a record: it didn't exit in case of delete, so it re-create the document (the right way in case of update task)

@njordr njordr closed this as completed Dec 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants