简体   繁体   English

使用pymongo时是否存在类似的关系函数?

[英]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. 类别是唯一的(因为我使用upsert),或者我们还是承认它是类别。

A collection named rules that contains documents like so: 一个名为rules的集合,其中包含如下文档:

{
    "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']. 因此,我可以从“ 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. 1)我希望该过程在数据库端进行。 Like categories.find_one().distinct('path').where(cat=rule['cat'])? 像category.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? 2)有没有一种方法可以定义一种关系,这样我就不需要重复cat字段了?

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. 对于1),您根本不想在这里或实际上一直使用服务器端javascript。 It's slow and blocks lots of other operations. 它很慢并且会阻止许多其他操作。 Don't use server-side javascript to try and fake joins in MongoDB. 不要使用服务器端JavaScript尝试在MongoDB中伪造联接。

For 2), duplicating the path info into the rules documents seems like the best solution. 对于2),将path信息复制到rules文档中似乎是最好的解决方案。 How often will a path change? path多久更改一次? 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. 与当前设置相比,嵌入path的成本是复制,并且如果path发生更改,则需要一组更昂贵的更新。 Seems worth it to me, in the absence of further information about your use case. 在没有有关您的用例的进一步信息的情况下,对我而言这似乎是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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