这只是一个函数,用于更新mongo集合内数组中的单个字段。 Modify函数工作正常,关键是未修改返回文档,是更新过程之前的文档。
archiveAnnounce: function (database, operation, id, orchestra, callback) {
database.collection(orchestra).findOneAndUpdate(
{storage: 'announces', "list.when": id},
{$set: {"list.$.archived": operation}},
{returnNewDocument: true},
function (err, doc) {
console.log(doc);
doc.list.sort(function (a, b) {
return parseFloat(b.when) - parseFloat(a.when);
});
callback(err, doc.list);
}
);
},
{ lastErrorObject: { n: 1, updatedExisting: true },
value: { _id: 5a0d9acfbaeddce881617d4c,
storage: 'announces',
list: [ { title: 'Esto es una prueba',
author: 'smartnexus',
desc: 'Qasdsadasdsadadasd\n\n*adqdqdqsknnoq coq',
when: '1537097127638',
archived: false } ] },
ok: 1 }