我正在插入這樣的新記錄(Book是模型):
var ext = 'pdf'
var dataToInsert = {
'author': 'ABC',
'country': 'US',
'file_name': ''
}
var new_book = new Book( dataToInsert );
await new_book.save();
const file_name = new_book._id '.' ext
//-- update the document with the new file_name
在這里,不是findOneAndUpdate()用于更新file_name欄位,是否有更好的方法,例如一次性完成?
uj5u.com熱心網友回復:
你可以試試這個
var ext = 'pdf'
var dataToInsert = {
'author': 'ABC',
'country': 'US',
'file_name': ''
}
var new_book = new Book( dataToInsert );
// create mongo id before saving and use that id for file_name creation
new_book._id = mongoose.Types.ObjectId()
new_book.file_name = new_book._id '.' ext
await new_book.save();
uj5u.com熱心網友回復:
下面的代碼可能對你有幫助。
const ext = 'pdf'
const dataToInsert = {
'author': 'ABC',
'country': 'US',
'file_name': ''
}
const new_book = new Book( dataToInsert );
await new_book.save();
new_book.file_name = new_book._id '.' ext;
new_book.save();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394736.html
