简体   繁体   中英

Is there a where like relation function when using pymongo?

I have the following data:

A collection named categories that contains documents like so:

{
    "cat":"Films Anglais",
    "path":"W:\\videos"
}

Categories are unique (because I use upsert) or let's admit it is anyway.

A collection named rules that contains documents like so:

{
    "title":"braveheart",
    "regex":"^.*braveheart.*$",
    "cat":"Films Anglais"
}

I am iterating on all the rules. So I can access the cat from rules as rule['cat']. What I need is the path from categories.

I know that I can do:

dest = ""
for category in categories.find():
    if category['cat'] == rule['cat']:
        dest = category['path']
        break

1) I would prefer the process to be database side. Like categories.find_one().distinct('path').where(cat=rule['cat'])? Invalide I know.

2) Is there a way to define a sort of relation so that I do not need to duplicate the cat field?

Lastly, I have read about the difference between relationnal and non relationnal systems but in this case the choice is sealed.

For 1), you don't want to use server-side javascript here, or ever, really. It's slow and blocks lots of other operations. Don't use server-side javascript to try and fake joins in MongoDB.

For 2), duplicating the path info into the rules documents seems like the best solution. How often will a path change? The cost to embedding path is the duplication and the need for a more expensive set of updates if a path changes, as compared to your current setup. Seems worth it to me, in the absence of further information about your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM