基于深度嵌套对象单字段的morphia查询
我想根据与我要检索的对象嵌套了 2 层的对象的 id(或其他单个字段)来检索对象。演示的一个例子:
我想查找特定用户评论过的所有博客文章。
Blog
List<Comment>
ignoredField1
ignoredField2
User
id
name
ignoredField3
评论和用户由其父对象@Referenced。
读完这篇文章后 http://groups.google.com/group/morphia/browse_thread /thread/57090ef1bd2f3e74?pli=1
我明白如何找到带有评论的博客,其中ignoreField1/2具有特定值,但我想走得更远。
我已尝试以下操作,但由于比较了所有评论字段,因此没有匹配项
q.field("comments").hasThisElement(new Comment(new User("name")));
I want to retrieve an object, based on an id (or other single field) of an object that is nested 2 levels from the object that I want to retrieve. An example to demonstrate:
I want to find all blog posts that have been commented on by a particular user.
Blog
List<Comment>
ignoredField1
ignoredField2
User
id
name
ignoredField3
Comments and Users are @Referenced by their parent objects.
After reading this post
http://groups.google.com/group/morphia/browse_thread/thread/57090ef1bd2f3e74?pli=1
I understand how i would find blogs with comments where ignoredField1/2 has a particular value, but i want to navigate further than that.
I have tried the following but because all Comment fields are compared, there is no match
q.field("comments").hasThisElement(new Comment(new User("name")));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须通过几个步骤来完成:
获取用户的对象 ID
获取该用户的所有评论
获取包含这些评论的所有博客..
但是,还有更好的方法:
嵌入注释而不是引用它们。它们作为一个独立的对象没有多大意义。然后你可以这样做:
将评论中的引用添加回博客 - 例如,名为“blog”的 ObjectId 字段。然后你可以这样做:
获取所有博客对象Id,然后在下一步中加载它们。
You'll have to do it in a fews of steps I think:
Get the user's object ID
Get all comments with that user
Get all Blogs with those comments..
However, there are better ways:
Embed Comments rather than reference them. They don't make much sense as a standalone object. Then you can do:
Add a reference from Comments back to Blog - an ObjectId field called "blog" for example. Then you can do:
to get all the Blog object Ids, then load them in a further step.