Мое определение схемы выглядит следующим образом:
var nSchema = new Schema({
message: String,
creationDate: { type : Date, default: Date.now }
});
var sSchema = new Schema({
_id: {type:ObjectId, auto: true },
dataId: { type:String, unique: true },
notes: [nSchema]
}
Для каждого dataId
может быть несколько notes []
записей.
Я пишу функцию для поиска последних message
из notes[]
. Последнее сообщение определяется на основе creationDate
SData.findOne({ dataId: dataId}, function (err, sAudit) {
var note = sAudit.notes.filter(function (note) {
return note.message;
}).pop();
console.log(note);
});