如何检查日期和时差mongodb聚合并推入数组

时间:2018-12-13 11:16:46

标签: mongodb mongodb-query

我这里有三个桌子

  1. 组织
  2. GroupContents

我想检查$match的条件并连接两个表(GroupsGroupContents)。到目前为止,一切正常,我的问题是GroupContents集合我有两个分别称为activationDatedeActivationDate的字段。我想检查这些日期的时差并推入Topic数组。假设deActivationDate日期 null 表示为今天。

  

组织

{
    "_id" : ObjectId("5c11efebd9cb4d35f47d6bd0"),
    "orgID" : "ORG1",
    "name" : "The Punjab Public School",
    "oldOrgID" : "176348"
}
  

/* 1 createdAt:12/13/2018, 11:06:02 AM*/
{
    "_id" : ObjectId("5c11efc2d9cb4d35f47d6bcf"),
    "groupID" : "2",
    "name" : "8 B",
    "otherIds" : {
        "orgID" : "ORG1"
    },
    "version" : NumberInt(1)
},

/* 2 createdAt:12/13/2018, 11:05:08 AM*/
{
    "_id" : ObjectId("5c11ef8cd9cb4d35f47d6bce"),
    "groupID" : "1",
    "name" : "8 A",
    "otherIds" : {
        "orgID" : "ORG1"
    },
    "version" : NumberInt(1)
}
  

GroupContents

/* 1 createdAt:12/13/2018, 4:20:08 PM*/
{
    "_id" : ObjectId("5c123960d9cb4d35f47d6bd4"),
    "groupID" : "2",
    "pedagogyID" : "200",
    "activationDate" : "2018-08-24 14:13:45",
    "deActivationDate" : "2018-08-25 18:25:45",
    "contentState" : "A",
    "status" : "assigned"
},

/* 2 createdAt:12/13/2018, 11:07:57 AM*/
{
    "_id" : ObjectId("5c11f035d9cb4d35f47d6bd2"),
    "groupID" : "1",
    "pedagogyID" : "200",
    "activationDate" : "2018-08-24 14:13:45",
    "deActivationDate" : "2018-08-30 18:25:45",
    "contentState" : "A",
    "status" : "assigned"
},

/* 3 createdAt:12/13/2018, 11:07:29 AM*/
{
    "_id" : ObjectId("5c11f019d9cb4d35f47d6bd1"),
    "groupID" : "1",
    "pedagogyID" : "100",
    "activationDate" : "2018-08-20 18:25:45",
    "deActivationDate" : null,
    "contentState" : "A",
    "status" : "assigned"
}
  

我的汇总代码

var schoolCode = "176348";
  db.Organizations.aggregate([
    // Stage 1      
  {
    $match: {
      oldOrgID: schoolCode
    }
  },
  // Stage 2
  {
    $lookup: {
      from: "GroupContents",
      localField: "otherIds.orgID", // Groups table orgID
      foreignField: "orgID", // GroupContents table orgID
      as: "Topic"
    }
  },
  {
    $project: {
      "orgID": 1,
      "name": 1,
      "Topic.groupID": 1, 
      "Topic.pedagogyID": 1, 
      "Topic.activationDate": 1, 
      "Topic.deActivationDate": 1 
    }
  }
])
  

我的输出

{
    "_id" : ObjectId("5c11efebd9cb4d35f47d6bd0"),
    "orgID" : "ORG1",
    "name" : "The Punjab Public School",
    "Topic" : [
        {
            "groupID" : "1",
            "pedagogyID" : "100",
            "activationDate" : "2018-12-10 18:25:45",
            "deActivationDate" : null
        },
        {
            "groupID" : "1",
            "pedagogyID" : "200",
            "activationDate" : "2018-12-10 14:13:45",
            "deActivationDate" : "2018-12-12 18:25:45"
        },
        {
            "groupID" : "2",
            "pedagogyID" : "200",
            "activationDate" : "2018-12-12 14:13:45",
            "deActivationDate" : "2018-12-15 18:25:45"
        }
    ]
}
Expected Output
{
    "_id" : ObjectId("5c11efebd9cb4d35f47d6bd0"),
    "orgID" : "ORG1",
    "name" : "The Punjab Public School",
    "Topic" : [
        {
            "groupID" : "1",
            "pedagogyID" : "100",
            "activationDate" : "2018-12-10 18:25:45",
            "deActivationDate" : null,
            "differenceindays" : 4
        },
        {
            "groupID" : "1",
            "pedagogyID" : "200",
            "activationDate" : "2018-12-10 14:13:45",
            "deActivationDate" : "2018-12-12 18:25:45",
            "differenceindays" : 3
        },
        {
            "groupID" : "2",
            "pedagogyID" : "200",
            "activationDate" : "2018-12-12 14:13:45",
            "deActivationDate" : "2018-12-15 18:25:45",
            "differenceindays" : 4
        }
    ]
}

0 个答案:

没有答案