MongoEngine 中的子查询

发布于 2024-10-16 23:53:24 字数 564 浏览 7 评论 0原文

有示例代码:

import mongoengine as mongo

class User(mongo.Document):
    name = mongo.StringField()
    age = mongo.IntField()    

class Post(mongo.Document):
    title = mongo.StringField()
    author = mongo.ReferenceField(User)

我想获取年龄在30岁以下的用户创建的所有帖子,一种方法是分两步发出请求:

users = User.objects(age__lt=30)
posts = Post.objects(author__in=users)

但即使这也不是完全糟糕,它不必要地调用和连接数据库N次。所以我想一步完成查询,我尝试了:

posts = Post.objects(author__age__lt=30)

但是不起作用,它只是返回给我一个空列表,而不是错误。我做错了什么?

Having the sample code:

import mongoengine as mongo

class User(mongo.Document):
    name = mongo.StringField()
    age = mongo.IntField()    

class Post(mongo.Document):
    title = mongo.StringField()
    author = mongo.ReferenceField(User)

I want to get all the posts created by users with ages below 30, one way is to make the request in two steps:

users = User.objects(age__lt=30)
posts = Post.objects(author__in=users)

But even this is not totally bad, it is calling and connecting to the database N times unnecessarily. So I want to make the query in just one step, I tried:

posts = Post.objects(author__age__lt=30)

But does not work, it just returns me a empty list, not an error. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2024-10-23 23:53:24

我做错了什么?

没有什么是不能跨参考字段查询的 - 请参阅嵌入与参考< /a>

因此,即使支持该语法,也需要在幕后进行两次查询。如果它是想要的东西 - 在 github 上添加一张票,我会考虑添加它:)

MongoEngine 的更新目前正在密集而快速地到来 - 所以新版本即将发布! dev 分支上有一些令人兴奋的改进,包括高效的惰性取消引用和用于原子保存的更新增量!

What am I doing wrong?

Nothing you can't query across reference fields - see Embed Vs Reference

So even if the syntax was supported it would take two queries behind the scenes. If its something thats wanted - add a ticket to github and I'll look into adding it :)

Updates to MongoEngine are coming in thick and fast at the moment - so a new release will be coming soon! Some exciting improvements are on the dev branch, including efficient lazy dereferencing and update deltas for atomic saving!

来日方长 2024-10-23 23:53:24

...它不必要地调用并连接数据库 N 次

MongoDB 支持 $in 子句,该子句允许仅通过一次查询、一次连接来选择多个对象。如果您看到多个连接,您可能应该将此报告给 mongoengine 的作者。

因为 MongoDB 不支持连接,所以您的“加载用户,加载他们的帖子”的方法通常是正确的方法。

我做错了什么?

对于这种级别的详细信息,您最好的选择是直接联系作者。 github 页面上有他的电子邮件和博客。查看 repo,他的最后一次更新是 2010 年 10 月,最后版本号是 0.4。三个月没有更新对于这个领域来说是很长的时间,而且他甚至没有 1.0 版本,所以你很可能需要直接与作者交谈。

... it is calling and connecting to the database N times unnecessarily

MongoDB supports an $in clause that allows for the selection of multiple objects with only one query, one connection. If you're seeing multiple connections, you should probably report this to the author of mongoengine.

Because MongoDB does not support joins, your method of "load users, load their posts" is generally the correct method.

What am I doing wrong?

For this level of detail, your best bet is to contact the author directly. The github page has both his e-mail and his blog. Looking at the repo, his last update was October 2010 and the last version number was 0.4. Three months with no update is a long time in this space and he doesn't even have a 1.0 version, so it's quite likely that you'll need to speak with the author directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文