0

hello I am a mongoDB noob, And I wanted to retrieve a list of comments on a post:

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

how can I query only the comments array of a post?

I dont want to retrieve the post with the comments inside it, but only the comments without anything else?

here is my first attempt with jongo :

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

this doesn't work :/, I waas thinking about casting the comments array to a Comment type. and using the projection to only retrieve the comments part...

popo joe
  • 910
  • 1
  • 9
  • 24

1 Answers1

0

This will work on mongo shell(map it to Jongo API),

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

Refer to this link for details:querying in mongodb with limited fields

vmr
  • 1,895
  • 13
  • 24