0

I am new to node.js.
I am having JSON object of the form

{ "_id" : ObjectId("540b03ddf1768fe562fbb715"),
"productid" : "men1",
"comments" : [ { "name" : "shiva", "text" : "Haidddsdcccccc", "_id" : ObjectId("540b03dd0570a6261e20a59e"), "date_entered" : ISODate("2014-09-06T12:53:49.658Z") },
{ "name" : "shiva", "text" : "Haidddsdcccccc", "_id" : ObjectId("540cb2be35f8145a2d296ea0"), "date_entered" : ISODate("2014-09-07T19:32:14.827Z") },
{ "name" : "shiva", "text" : "Haidddsdcccccc", "_id" : ObjectId("540cb2c335f8145a2d296ea1"), "date_entered" : ISODate("2014-09-07T19:32:19.456Z") } ] }

I want to query comments of a product after a specific time

I am using query as

var query=Product.find({"productid":obj.productid,
'comments.date_entered': {$gt: obj.timed}}, {'comments.$': 1})

I am getting only one object after the specific time i.e

{
 _id: "540b03ddf1768fe562fbb715"
 comments: [1]
 0:  {
 name: "shiva"
 text: "Haidddsdcccccc"
 _id: "540cb2be35f8145a2d296ea0"
 date_entered: "2014-09-07T19:32:14.827Z"
 }

}

How to get all the comments of the product after the specified time?

Vishnu
  • 8,649
  • 3
  • 41
  • 73
  • Have a look at the answers to this question: http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – JohnnyHK Sep 08 '14 at 14:22
  • Thanks for the suggestion :-) – Vishnu Sep 08 '14 at 19:18

1 Answers1

1

Doesn't seem to be a good way to do this from what I can find out. A work-around could be doing something like this:

Product.findOne({productid: PRODUCT_ID}, {comments: 1}, function(err, product) {
    var comments = product.comments.filter(function(comment) {
        return comment.date_entered > DATE;
    });
    ...
})
Mattias Farnemyhr
  • 3,968
  • 3
  • 26
  • 45