Ordina dal più recente, raggruppa in array, filtra i nulli, poi prendi la testa di ciascuno:
db.getCollection('test').aggregate([ // Sort by timestamp, descending { $sort: { datetime: -1 } }, // Collect each field into an array { $group: { "_id": null, "field1": { $push: "$field1" }, "field2": { $push: "$field2" } }}, // Drop null entries { $project: { "field1notNull": { $filter: { input: "$field1", as: "f", cond: { $ne: [ "$$f", null ] } }}, "field2notNull": { $filter: { input: "$field2", as: "f", cond: { $ne: [ "$$f", null ] } }} }}, // Take the first surviving value of each { $project: { "_id": null, "field1": { $arrayElemAt: ["$field1notNull", 0] }, "field2": { $arrayElemAt: ["$field2notNull", 0] } }} ]) Il $sort decrescente nel primo stadio è ciò che rende corretto il tutto — $arrayElemAt con indice 0 seleziona poi il valore non nullo più recente anziché uno arbitrario.
Tieni presente che $group con _id: null bufferizza in memoria ogni valore dei campi, cosa che non scala su collezioni grandi. Per dataset più estesi, un $push del solo timestamp e del campo, seguito da $sort e da un $group con $first, mantiene il working set più piccolo.