Как увеличить значение на основе двух дат в mongodb - PullRequest
0 голосов
/ 14 декабря 2018

Я делаю запрос агрегации в mongodb, и я использую foreach.внутри foreach я печатаю print(doc); документ означает, что я получаю ниже документы.

Мой запрос

var schoolData = {};
var orgID = "5a298014421aa95214722fa4";
var millisecondsPerDay = 1000 * 60 * 60 * 24;
db.Organizations.aggregate([
  // Stage 1     
  {
    $match: {
      orgID: orgID
    }
  },

  // Stage 2    
  {
    $lookup: {
      from: "Groups",
      localField: "orgID", // organization table orgID
      foreignField: "otherIds.orgID", //Grops table orgID
      as: "TopicDetails"
    }
  },
  // Stage 3
  {
    $unwind: {
    path: "$TopicDetails",
    // "difference" :{$subtract:["$TopicDetails.deActivationDate", "$TopicDetails.activationDate"]},
    preserveNullAndEmptyArrays: true
    }

  },
   // Stage 4   
  {
    $lookup: {
      from: "GroupContents",
      localField: "TopicDetails.groupID", // Grops table groupID
      foreignField: "groupID", //GroupContents table orgID
      as: "TopicDetails"
    }
  },
  {
    $project: {
     "orgID": 1,
      "name": 1,
      "TopicDetails.name": 1,
      "TopicDetails.groupID": 1,
      "TopicDetails.pedagogyID": 1,
      "TopicDetails.activationDate": 1,
      "TopicDetails.deActivationDate": 1,
       //"difference" :{$subtract:["$last:$TopicDetails.deActivationDate", "$last:$TopicDetails.activationDate"]}
    }
    /*,
    {
     $unwind:"$TopicDetails"
    }*/
  }

])
.forEach(function(doc){
  print(doc);
    var date1 = new Date('2013/11/04 00:00:00');
    var date2 = new Date('2013/11/05 10:10:10'); //less than 1
    var start = Math.floor(date1.getTime() / (3600 * 24 * 1000)); //days as integer from..
    var end = Math.floor(date2.getTime() / (3600 * 24 * 1000)); //days as integer from..
    var daysDiff = end - start; // exact dates
    console.log(daysDiff);
});

print (doc);

    {
    "_id" : "5a298014421aa95214722fa4",
    "orgID" : "5a298014421aa95214722fa4",
    "name" : "Demo School",
    "TopicDetails" : [
        {
            "groupID" : "5a33db8b9bcf37062d72efe1",
            "pedagogyID" : "5a33e1bc9bcf3706dc3aae62",
            "activationDate" : "2018-05-06 20:01:43",
            "deActivationDate" : "2018-05-05 14:18:52"
        },
        {
            "groupID" : "5a33db8b9bcf37062d72efe1",
            "pedagogyID" : "5a33e1a29bcf3706cc658c87",
            "activationDate" : "2017-12-15 20:22:18",
            "deActivationDate" : null
        },
        {
            "groupID" : "5a33db8b9bcf37062d72efe1",
            "pedagogyID" : "5a33e15f9bcf3706cd2794e0",
            "activationDate" : "2017-12-15 20:21:11",
            "deActivationDate" : null
        }
    ]
}
{
    "_id" : "5a298014421aa95214722fa4",
    "orgID" : "5a298014421aa95214722fa4",
    "name" : "Demo School",
    "TopicDetails" : [
        {
            "groupID" : "5a33db909bcf37063f2af532",
            "pedagogyID" : "5a97982a9bcf377629344c24",
            "activationDate" : "2018-03-01 11:35:30",
            "deActivationDate" : "2018-05-08 19:40:39"
        },
        {
            "groupID" : "5a33db909bcf37063f2af532",
            "pedagogyID" : "5a9791f99bcf3776f521ef48",
            "activationDate" : "2018-03-01 11:09:05",
            "deActivationDate" : null
        }
    ]
}

внутри forloop я вычисляю разницу между двумя датами и сохраняю в одну переменную с именем daysDiff.Как я могу вставить эту переменную в мой массив TopicDetails?

Ожидаемый результат.

Примечание: внутри цикла foreach я даю статические даты, на самом деле, я должен передать TopicDetails.activationDate & TopicDetails.deActivationDate на основе этой даты daysDiff значения будут различаться

{
    "_id" : "5a298014421aa95214722fa4",
    "orgID" : "5a298014421aa95214722fa4",
    "name" : "Demo School",
    "TopicDetails" : [
        {
            "groupID" : "5a33db8b9bcf37062d72efe1",
            "pedagogyID" : "5a33e1bc9bcf3706dc3aae62",
            "activationDate" : "2018-05-06 20:01:43",
            "deActivationDate" : "2018-05-05 14:18:52",
            "daysDiff" : 2
        },
        {
            "groupID" : "5a33db8b9bcf37062d72efe1",
            "pedagogyID" : "5a33e1a29bcf3706cc658c87",
            "activationDate" : "2017-12-15 20:22:18",
            "deActivationDate" : null,
            "daysDiff" : 2
        },
        {
            "groupID" : "5a33db8b9bcf37062d72efe1",
            "pedagogyID" : "5a33e15f9bcf3706cd2794e0",
            "activationDate" : "2017-12-15 20:21:11",
            "deActivationDate" : null,
            "daysDiff" : 2
        }
    ]
}
{
    "_id" : "5a298014421aa95214722fa4",
    "orgID" : "5a298014421aa95214722fa4",
    "name" : "Demo School",
    "TopicDetails" : [
        {
            "groupID" : "5a33db909bcf37063f2af532",
            "pedagogyID" : "5a97982a9bcf377629344c24",
            "activationDate" : "2018-03-01 11:35:30",
            "deActivationDate" : "2018-05-08 19:40:39",
            "daysDiff" : 2
        },
        {
            "groupID" : "5a33db909bcf37063f2af532",
            "pedagogyID" : "5a9791f99bcf3776f521ef48",
            "activationDate" : "2018-03-01 11:09:05",
            "deActivationDate" : null,
            "daysDiff" : 2
        }
    ]
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...