Mongodb查询子列表

时间:2014-02-07 11:39:36

标签: mongodb jongo

你好,我是一个mongoDB菜鸟, 我想在帖子上检索一个评论列表:

{
  id:0,
  ref:0,
  type: 'image',
  date: null,
  title: 'this is my title',
  comments:[
      {
        user : 'myUser',
        text : 'text'
      },
      {
        user : 'myUser2',
        text : 'text2'
      }
}

如何只查询帖子的评论数组?

我不想检索包含其中的评论的帖子,但只想查看没有其他内容的评论?

这是我对jongo的第一次尝试:

Post.posts()。find(“{ref:#}”,ref).projection(“{comments:1}”)。as(Post.Comment.class)

这不起作用:/,我在考虑将comments数组转换为Comment类型。 并使用投影仅检索评论部分......

1 个答案:

答案 0 :(得分:0)

这将适用于mongo shell(将其映射到Jongo API),

db.posts.find({}, { comments: 1, _id: 0 });

有关详细信息,请参阅此链接:querying in mongodb with limited fields

相关问题