我的目标是: 1.创建一个文档-我需要确保该文档有效,并且实际上已保存到数据库中,因为其_id可能会在同一集合中嵌入其他文档。 2.保存后,我需要针对集合中的所有其他文档运行此文档,以查找哪些文档_id本身嵌入。 3.然后,我想更新此文档并保存。
我已经看了好几个小时的文档。
我了解到,当我编辑保存或提取的文档时,猫鼬会看到更改并触发updateOne()
Mongoose documents track changes. You can modify a document using vanilla JavaScript assignments and Mongoose will convert it into MongoDB update operators.
问题1是在对象上触发的updateOne()是异步的,如果我运行批量文档创建,则会收到VersionError警告。有没有办法使这个异步/等待?
另一个选项是手动运行updateOne()。但是文档指出save()是正确的更新方式。我找不到最佳实践。
The save() function is generally the right way to update a document with Mongoose. With save(), you get full validation and middleware.
在编辑文档时,如何在不触发标准updateOne()的情况下检索和编辑文档?
尝试1
// check if already exists
const existsCheck = await getCard(null, card.name);
if (existsCheck) {
throw new Error(`Duplicate item, ${existsCheck.name} already exists: [${existsCheck.id}]`);
}
console.log(`Creating card ${card.name}`);
const baseCard = new Card( card );
baseCard.synergiespre = stringToArray(baseCard.synergiespre, ',');
baseCard.antisynergiespre = stringToArray(baseCard.antisynergiespre, ',');
// baseCard.markModified('synergiespre');
// baseCard.markModified('antisynergiespre');
// create card to:
// 1. Get an id
// 2. Ensure save() occurs before creating item synergies editing other item synergies
await baseCard.save();
const updatedCardData = await compileSynergiesAndAntiSynergies(baseCard);
baseCard.synergies = await updatedCardData.synergies; // doesnt await
baseCard.antisynergies = await updatedCardData.antisynergies; // doesnt await
const returnPopulatedCard = await getCard(baseCard.id);
console.log('Populated Return');
console.log(returnPopulatedCard);
return returnPopulatedCard;
在没有等待的情况下,我收到了VersionError,因为有很多异步更改
尝试2
// check if already exists
const existsCheck = await getCard(null, card.name);
if (existsCheck) {
throw new Error(`Duplicate item, ${existsCheck.name} already exists: [${existsCheck.id}]`);
}
console.log(`Creating card ${card.name}`);
let baseCard = new Card( card );
baseCard.synergiespre = stringToArray(baseCard.synergiespre, ',');
baseCard.antisynergiespre = stringToArray(baseCard.antisynergiespre, ',');
// baseCard.markModified('synergiespre');
// baseCard.markModified('antisynergiespre');
// create card to:
// 1. Get an id
// 2. Ensure save() occurs before creating item synergies and editing other item synergies
baseCard = await baseCard.save();
const updatedCardData = await compileSynergiesAndAntiSynergies(baseCard);
baseCard.synergies = await updatedCardData.synergies;
baseCard.antisynergies = await updatedCardData.antisynergies;
baseCard = await baseCard.save();
const returnPopulatedCard = await getCard(baseCard.id);
console.log('Populated Return');
console.log(returnPopulatedCard);
return returnPopulatedCard;
VersionError,因为我尝试按照建议的方式保存()。我应该如何获取文档并进行编辑,然后保存()而又不触发updateOne()
调试日志
Mongoose:
cards.updateOne(
{ _id: ObjectId("5cfef0ad6037ee55f17d4ee5"), __v: 0 },
{ '$set': {
antisynergies: [ ObjectId("5cfeea1840b4cc558e14a221") ],
synergies: [
ObjectId("5cfeea0e40b4cc558e149e90"),
ObjectId("5cfeea1040b4cc558e149ed6"),
ObjectId("5cfeea1040b4cc558e149ee1"),
ObjectId("5cfeea1440b4cc558e14a07f"),
ObjectId("5cfeea1540b4cc558e14a0bc"),
ObjectId("5cfeea1540b4cc558e14a0cf"),
ObjectId("5cfef0ad6037ee55f17d4ee5") ],
updatedAt: new Date("Tue, 11 Jun 2019 00:07:10 GMT") },
'$inc': { __v: 1 } }, { session: null })
错误
{ VersionError: No matching document found for id "5cfef0ad6037ee55f17d4ee5" version 0 modifiedPaths "synergies, antisynergies"
at VersionError.MongooseError [as constructor] (/Applications/XAMPP/xamppfiles/htdocs/mlgserver/node_modules/mongoose/lib/error/mongooseError.js:14:11)
at new VersionError (/Applications/XAMPP/xamppfiles/htdocs/mlgserver/node_modules/mongoose/lib/error/version.js:18:17)
at generateVersionError (/Applications/XAMPP/xamppfiles/htdocs/mlgserver/node_modules/mongoose/lib/model.js:401:10)
at model.Model.save (/Applications/XAMPP/xamppfiles/htdocs/mlgserver/node_modules/mongoose/lib/model.js:459:28)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/mlgserver/dist/mongoose/spirespy/cards.js:416:39)
at Generator.next (<anonymous>)
at fulfilled (/Applications/XAMPP/xamppfiles/htdocs/mlgserver/dist/mongoose/spirespy/cards.js:4:58)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
message: 'No matching document found for id "5cfef0ad6037ee55f17d4ee5" version 0 modifiedPaths "synergies, antisynergies"',
name: 'VersionError',
version: 0,
modifiedPaths: [ 'synergies', 'antisynergies' ] }```