MongoDB:由created_at和sender组成

时间:2016-10-19 18:45:48

标签: node.js mongodb

消息集合结构
Message = new Schema
    type:           String
    body:           Schema.Types.Mixed
    sender:         { type: String, enum: ['machine', 'profile'], default: 'machine' }
    created_at:     { type: Date, default: Date.now }
    updated_at:     { type: Date, default: Date.now }

查询

> db.messages.find({},{created_at: 1, sender: 1, _id: 0})

返回

{ "created_at" : ISODate("2016-08-20T08:31:59.890Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-20T08:32:00.256Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-20T08:32:24.908Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-19T08:32:24.960Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-19T08:33:36.456Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-19T08:33:36.551Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-19T08:50:19.331Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-19T08:50:19.643Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-19T08:50:29.638Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-18T08:50:30.326Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-18T08:50:40.996Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-17T08:50:42.431Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-17T08:50:48.606Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-17T08:50:49.709Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-17T08:50:51.163Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-16T08:51:26.154Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-16T08:51:26.186Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-16T08:51:26.714Z"), "sender" : "machine" }
{ "created_at" : ISODate("2016-08-15T08:53:36.383Z"), "sender" : "profile" }
{ "created_at" : ISODate("2016-08-15T08:53:37.798Z"), "sender" : "machine" }

我需要一个查询的帮助,该查询将返回按日期和发件人分组的响应。

例如:日期= 2016-08-15 AND sender = profile,count = 2

数组:

profile: [{ 2016-08-12: 10}, { 2016-08-13: 10} , { 2016-08-14: 5}]

machine: [{ 2016-08-12: 10}, { 2016-08-13: 10} , { 2016-08-14: 5}]

需要帮助以Mongo友好格式起草

2 个答案:

答案 0 :(得分:2)

您应首先created_at sendersender,然后计算每个存储桶的数量。然后将[ {$group: { _id: { date: { $dateToString: { format: "%Y-%m-%d", date: "$created_at" } }, sender: '$sender' }, count: {$sum: 1} }}, {$group: { _id: '$_id.sender', arr: {$push: {date: '$_id.date', count: '$count'}} }} ] 分组,aggregate将日期计入数组。

<objectlist>
    <objectcode>OP#0003</objectcode>
    <objectid>0001</objectid>
    <objecttype>Test object</objecttype>
    <object>
        <info>
            <id>001</id>
            <name>Some name</name>
            <value>5</value>
        </info>
        <properties>
            <shopdetails>
                <desciption>
                    <header>Test</header>
                    <text>This is some text about the object</text>
                </desciption>
                <price>4</price>
                <currency>Dollar</currency>
                <weight>500</weight>
                <gramSymbol>mg</gramSymbol>
            </shopdetails>
        </properties>
    </object>
</objectlist>

答案 1 :(得分:1)

我会将ISODate投影到普通日期字符串,然后在datesender上分组

查询将如下所示:

db.collection.aggregate([
  // Normalize ISODate and remove time part.
  {$project:
    {created_at:
      {
        $dateToString:
        {format:"%Y-%m-%d", date:"$created_at"}
      }, 
      sender:1
    }
  },
  // Group documents by created_at, sender and calculate number of documents.
  {$group:
    {_id:
      {created_at:"$created_at", sender:"$sender"}, 
      count:{$sum: 1}
    }
  },
  // Sort result by created_at desc
  {$sort:
    {"_id.created_at":-1}
  },
  // Optional: $project to make result look pretty.
  {$project:
    {_id:0, created_at:"$_id.created_at", sender:"$_id.sender", count:1}
  }
])
  

注意:上一个$project阶段是可选的。

Out将是这样的:(我为了简洁而跳过完整的结果。

{ 
    "count" : NumberInt(1), 
    "created_at" : "2016-08-20", 
    "sender" : "machine"
}
{ 
    "count" : NumberInt(2), 
    "created_at" : "2016-08-20", 
    "sender" : "profile"
}
{ 
    "count" : NumberInt(3), 
    "created_at" : "2016-08-19", 
    "sender" : "profile"
}
{ 
    "count" : NumberInt(3), 
    "created_at" : "2016-08-19", 
    "sender" : "machine"
}
.
.
.