$ dateToString在mongodb中返回null和$ group

时间:2016-11-09 11:49:03

标签: mongodb datetime group-by mongodb-query tostring

我正在尝试使用id字段,datetime字段进行分组,并投影值和日期字段的总和。

db.getCollection('driver_collections').aggregate([
{$group : { _id: {driverid:"$driver_id",day:{ $dayOfMonth: "$time_in"},
                  month:{$month:"$time_in"},year:{$year:"$time_in"}},
    amountCollected : {$sum:"$total_collection"}}    
    },{
       $project: {
          yearMonthDay: { $dateToString: { format: "%Y-%m-%d", date: "$time_in" }},
          amountCollected : 1
       }
   }]) 

我使用了上面的查询并得到了结果

{
    "_id" : {
        "driverid" : "58014ce1f39e0f9dc40000b2",
        "day" : 30,
        "month" : 10,
        "year" : 2016
    },
    "amountCollected" : 2094.0,
    "yearMonthDay" : null
}

我需要将yearMonthDay作为

  

“yerMonthDay”:“2016-10-30”

当我使用 $ dateToString 而没有$ group时,它可以正常工作。我在这里想念的是什么

1 个答案:

答案 0 :(得分:1)

在第一阶段添加$ time_in

var pos = 0, test, testStatus, question, choice, choices, chA, chB, chC, correct = 0
  var questions = [
    [ 'What is 10 + 4?', '12', '14', '16', 'B' ],
    [ 'What is 20 - 9?', '7', '13', '11', 'C' ],
    [ 'What is 7 x 3?', '21', '24', '25', 'A' ],
    [ 'What is 8 / 2?', '10', '2', '4', 'C' ]
  ]
  function _ (x) {
    return document.getElementById(x)
  }
  function renderQuestion () {
    test = _('test')
    if (pos >= questions.length) {
      test.innerHTML = '<h2 class="mdl-typography--body-2">Você acertou ' + correct + ' de ' + questions.length + ' questões</h2>'
      _('testStatus').innerHTML = 'Teste terminado!'
      pos = 0
      correct = 0
      return false
    }
    _('testStatus').innerHTML = 'Questão ' + (pos + 1) + ' de ' + questions.length
    question = questions[pos][0]
    chA = questions[pos][1]
    chB = questions[pos][2]
    chC = questions[pos][3]
    test.innerHTML = '<h3 class="mdl-typography--body-2">' + question + '</h3>'

    // Construindo os radio buttons do MDL
    br = document.createElement('br')

    // The A alternative
    labelA = document.createElement('label')
    labelA.className = 'mdl-radio mdl-js-radio mdl-js-ripple-effect'
    labelA.setAttribute('for', 'optionA')
    labelA.setAttribute('id', 'labelA')

    inputA = document.createElement('input')
    inputA.setAttribute('type', 'radio')
    inputA.setAttribute('id', 'optionA')
    inputA.className = 'mdl-radio__button'
    inputA.setAttribute('name', 'choices')
    inputA.setAttribute('value', 'A')

    spanA = document.createElement('span')
    spanA.className = 'mdl-radio__label'
    textNodeA = document.createTextNode(chA)
    spanA.appendChild(textNodeA)

    labelA.appendChild(inputA)
    labelA.appendChild(spanA)
    componentHandler.upgradeElement(labelA)
    componentHandler.upgradeElement(inputA)
    componentHandler.upgradeElement(spanA)

    _('test').appendChild(labelA)
    _('test').appendChild(br)
    componentHandler.upgradeElement(_('test'))

    // The B alternative
    labelB = document.createElement('label')
    labelB.className = 'mdl-radio mdl-js-radio mdl-js-ripple-effect'
    labelB.setAttribute('for', 'optionB')
    labelB.setAttribute('id', 'labelB')

    inputB = document.createElement('input')
    inputB.setAttribute('type', 'radio')
    inputB.setAttribute('id', 'optionB')
    inputB.className = 'mdl-radio__button'
    inputB.setAttribute('name', 'choices')
    inputB.setAttribute('value', 'B')

    spanB = document.createElement('span')
    spanB.className = 'mdl-radio__label'
    textNodeB = document.createTextNode(chB)
    spanB.appendChild(textNodeB)

    labelB.appendChild(inputB)
    labelB.appendChild(spanB)
    componentHandler.upgradeElement(labelB)
    componentHandler.upgradeElement(inputB)
    componentHandler.upgradeElement(spanB)

    _('test').appendChild(labelB)
    _('test').appendChild(br)
    componentHandler.upgradeElement(_('test'))

    // The C alternative
    labelC = document.createElement('label')
    labelC.className = 'mdl-radio mdl-js-radio mdl-js-ripple-effect'
    labelC.setAttribute('for', 'optionC')
    labelC.setAttribute('id', 'labelC')

    inputC = document.createElement('input')
    inputC.setAttribute('type', 'radio')
    inputC.setAttribute('id', 'optionC')
    inputC.className = 'mdl-radio__button'
    inputC.setAttribute('name', 'choices')
    inputC.setAttribute('value', 'C')

    spanC = document.createElement('span')
    spanC.className = 'mdl-radio__label'
    textNodeC = document.createTextNode(chC)
    spanC.appendChild(textNodeC)

    labelC.appendChild(inputC)
    labelC.appendChild(spanC)
    componentHandler.upgradeElement(labelC)
    componentHandler.upgradeElement(inputC)
    componentHandler.upgradeElement(spanC)

    _('test').appendChild(labelC)
    _('test').appendChild(br)
    componentHandler.upgradeElement(_('test'))


    test.innerHTML += "<button class='mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--primary' onclick='checkAnswer()'>Enviar</button>"
  }
  function checkAnswer () {
    choices = document.getElementsByName('choices')
    for (var i = 0; i < choices.length; i++) {
      if (choices[i].checked) {
        choice = choices[i].value
      }
    }
    if (choice === questions[pos][4]) {
      correct++
    }
    pos++
    renderQuestion()
  }
  window.addEventListener('load', renderQuestion, false)