基于深度嵌套对象单字段的morphia查询

发布于 2024-12-25 18:48:30 字数 652 浏览 1 评论 0原文

我想根据与我要检索的对象嵌套了 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 技术交流群。

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

发布评论

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

评论(1

一花一树开 2025-01-01 18:48:30

我认为您必须通过几个步骤来完成:

  1. 获取用户的对象 ID

    ObjectId id = userObj.getId();
    
  2. 获取该用户的所有评论

    查询 q = ds.createQuery(Comment.class);
    q.field("用户").equal("姓名");
    q.retrievedFields(true, "_id"); // 只获取ID
    
  3. 获取包含这些评论的所有博客..

但是,还有更好的方法:

  1. 嵌入注释而不是引用它们。它们作为一个独立的对象没有多大意义。然后你可以这样做:

    查询 q = ds.createQuery(Blog.class);
    q.field("comments.user").equal("name");
    
  2. 将评论中的引用添加回博客 - 例如,名为“blog”的 ObjectId 字段。然后你可以这样做:

    查询 q = ds.createQuery(Comment.class);
    q.field("用户").equal("姓名");
    q.retrievedFields(true, "博客"); // 只获取博客ObjectIds
    

获取所有博客对象Id,然后在下一步中加载它们。

You'll have to do it in a fews of steps I think:

  1. Get the user's object ID

    ObjectId id = userObj.getId();
    
  2. Get all comments with that user

    Query q = ds.createQuery(Comment.class);
    q.field("user").equal("name");
    q.retrievedFields(true, "_id"); // just get the IDs
    
  3. Get all Blogs with those comments..

However, there are better ways:

  1. Embed Comments rather than reference them. They don't make much sense as a standalone object. Then you can do:

    Query q = ds.createQuery(Blog.class);
    q.field("comments.user").equal("name");
    
  2. Add a reference from Comments back to Blog - an ObjectId field called "blog" for example. Then you can do:

    Query q = ds.createQuery(Comment.class);
    q.field("user").equal("name");
    q.retrievedFields(true, "blog"); // only get the blog ObjectIds
    

to get all the Blog object Ids, then load them in a further step.

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