简体   繁体   English

查询猫鼬的子文档

[英]Query sub documents in mongoose

I am new to node.js. 我是node.js的新手。
I am having JSON object of the form 我有以下形式的JSON对象

{ "_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 ie 在特定时间后,我只能得到一个物体,即

{
 _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? 指定时间后如何获取产品的所有评论?

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;
    });
    ...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM