如何根据条件从mongoose中的文档中选择所选字段

时间:2017-02-03 13:02:09

标签: mongodb mongoose mongodb-query

我有这样的文档结构。我在哪里存储所有基于时间戳的事件。我的文档看起来像这样

[
  {
    "_id": {
      "$oid": "589341cff92be305c034cb5a"
    },
    "__v": 0,
    "name": "TV",
    "switch_event": [
      {
        "timestamp": 1486186277826,
        "event_type": "on"
      },
      {
        "timestamp": 1486272677826,
        "event_type": "off"
      },
      {
        "timestamp": 1486099877826,
        "event_type": "off"
      },
      {
        "timestamp": 1486186277826,
        "event_type": "on"
      },
      {
        "timestamp": 1486272677826,
        "event_type": "off"
      },
      {
        "timestamp": 1486099877826,
        "event_type": "off"
      }
    ]
  }
]

现在,在查询本文档时,我只对今天发生的事件感兴趣。所以在查询之后我正在写这样的投影查询(用于测试我保持时间戳> 0,这应该给所有事件) -

SwitchAppliance.find({_id:"589341cff92be305c034cb5a"},{
                name:1,
                switch_event:{$elemMatch: {
                    timestamp: {
                                $gt:0
                    }
                }}
         },(err,results)=>{
            if(err) {console.log(err);return next({message: "Internal Server Error"});}
            return res.json(results);
        } ); 

但是当我得到结果时,我只在switch_event数组中得到一个事件对象 - 就像这样 -

[
  {
    "_id": "589341cff92be305c034cb5a",
    "switch_event": [
      {
        "_id": "589567251c653a0890b8b1ef",
        "event_type": "on",
        "timestamp": 1486186277826
      }
    ],
    "name": "TV"
  }
]

2 个答案:

答案 0 :(得分:0)

您正在查询Date.now()(时间戳或号码)与new Date()(日期对象)。快速控制台测试显示以下内容:

> typeof Date.now()
=> "number"
> typeof new Date()
=> "object"

首先,我会改变

var today = new Date();

var today = Date.now();

然后您缺少“timestamp”不包含日期值,而是另一个Object,其中一个Item的键为$date,日期为值。因此,您的查询应该看起来类似于以下内容:

var today = Date.now();
Item1.find({}).populate({
  path:"item1",
  select: {
    name:1,
    event:{
      $elemMatch: {
        timestamp: {
          date: {
            $gte:today
          }
        }
      }
    }
  }
})

答案 1 :(得分:0)

最后,在这里找到了关键。

Unwind empty array in mongodb

这里@ ivan.srb的答案很方便。 最后结束了这样的事情 -

SwitchAppliance.aggregate([
                {$match:{_id:{$in : switch_appliances_array}}},

                {$project:{
                   switch_no:1,switch_name:1,switch_type:1,appliance_type:1,sboard_id:1,current_status:1,
                   switch_event : { $cond : [ { $eq : [ "$switch_event", [] ] }, [ { timestamp : 0 , event_type:"off"} ], '$switch_event' ] } 
                }},
                {$unwind:"$switch_event"},

                {$match:{$or: [{"switch_event.timestamp":{$gt:now}},{ "switch_event.timestamp":0} ]}},

                {$group:{
                    _id:"$_id",
                    switch_no:{$first:"$switch_no"},
                    switch_type:{$first:"$switch_type"},
                    switch_name:{$first:"$switch_name"},
                    appliance_type:{$first:"$appliance_type"},
                    current_status:{$first:"$current_status"},
                    switch_events:{$push:"$switch_event"}
                }},