BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.04k stars 215 forks source link

Feature Request - Check Link Validity #742

Open mg3146 opened 1 year ago

mg3146 commented 1 year ago

Hi,

Is there an efficient way to check if a link is still valid without having the fetch the linked document ?

Thx.

mike-pisman commented 1 year ago

I'm not an expert, but I don't think there is a more efficient way(if that's what you're looking for), unless you keep a list of document IDs manually: you can prefetch list of all documents at the start of the application, save IDs to the list and then set up even-based actions to updated that list.

To answer your question... you can get the document using it's reference ID. i.e.

Document.get(link.ref.id)

You can also search for document and use count method. https://beanie-odm.dev/api-documentation/query/#findonecount

Would be something like

Document.find_one(link.ref.id).count() == 1