MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Aggrega i risultati di MongoDB per data ObjectId

Aggiornamento:ora esiste un modo integrato per farlo, vedi https://stackoverflow.com/a /51766657/295687

Non c'è modo di ottenere ciò che stai chiedendo con il framework di aggregazione di mongodb, perché non esiste un operatore di aggregazione in grado di trasformare ObjectId in qualcosa di simile a una data (c'è un JIRAticket , anche se). Tuttavia, dovresti essere in grado di ottenere ciò che desideri utilizzando map-reduce:

// map function
function domap() {
    // turn ObjectId --> ISODate
    var date = this._id.getTimestamp();
    // format the date however you want
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();

    // yields date string as key, entire document as value
    emit(year+"-"+month+"-"+day, this);
}

// reduce function
function doreduce(datestring, docs) {
    return {"date":datestring, "docs":docs};
}