文書をmongoコレクションに保存する必要があります。
私は「insertedAt」と「updatedAt」日付フィールドを保存したいので、私は、私は1つのステップでそれを行うことができないと仮定し...マングース:文書を更新/保存するにはどうしたらいいですか?
これが私の最後の試みです:
my topic = new Topic(); // Topic is the model
topic.id = '123'; // my univocal id, !== _id
topic.author = 'Marco';
...
Topic.findOne({ id: topic.id }, function(err, doc) {
if (err) {
console.error('topic', topic.id, 'could not be searched:', err);
return false;
}
var now = new Date();
if (doc) { // old document
topic.updatedAt = now;
} else { // new document
topic.insertedAt = now;
}
topic.save(function(err) {
if (err) {
console.error('topic', topic.id, 'could not be saved:', err);
return false;
}
console.log('topic', topic.id, 'saved successfully');
return true;
});
});
しかし、その後、私はレコードを複製してしまうこのよう... :-(
任意の提案?スキーマ定義ではfalseに
新しいデータベースレコードを保存しない場合は、 '新しいトピック 'を作成しないでください。ちょうど 'find {_id:...'、 'doc.updatedAt = now'と' doc.save' – wostex