大佬教程收集整理的这篇文章主要介绍了MongoError:更新路径“publications.$[pub].updatedAt”会在“publications.$[pub]”处产生冲突,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是数据库中对象的结构。我需要嵌套模式,因为数组的嵌套对象可能会在不同时间更新,我需要知道是否会发生这种情况。
const PublicationscheR_474_11845@a = new scheR_474_11845@a({
ID: String,data: String,},{timestamps: true,_ID: false]);
const AuthorscheR_474_11845@a = new scheR_474_11845@a({
ID: String,publications: [PublicationscheR_474_11845@a]
},{ timestamps: true,_ID: falsE});
AuthorModel.findOneAndupdate(
{
ID: authorID,"publications.ID": publicationID
},{
$set: {"publications.$[pub]": theNewObject
},{
arrayFilters: [{ 'publication.ID': publicationID }],new: true
},
我收到此错误:
updating the path 'publications.$[pub].updatedAt' would create a conflict at 'publications.$[pub]'
如果我有的话,我想提一下:
const AuthorscheR_474_11845@a = new scheR_474_11845@a({
ID: String,publications: [scheR_474_11845@a.Type.Mixed]
},_ID: falsE});
然后我的函数工作没有错误,但不包括 updatedAt 字段。那么如何使此更新适用于嵌套模式?
注意:即使在阅读了 Similar Question、Similar Question2 之后,我也不明白为什么会出现这个错误。
随着我越来越多地阅读 Similar Question,唯一的解决方法似乎是:
AuthorModel.findOneAndupdate(
{
id: authorId,"publications.id": publicationId
},{
$set: {
"publications.$[pub].id": theNewObject.id,"publications.$[pub].title": theNewObject.title,"publications.$[pub].content": theNewObject.content
}
},{
arrayFilters: [{ 'publication.id': publicationId }],new: true
}
}
这样你就不会产生updatedAt的冲突。在我的测试中,如果您更新数组中的一个项目,则只会获得新的 updatedAt。
如果您使用大对象并且不想设置每个字段,或者您事先不知道需要在嵌套对象中设置的所有字段,另一种解决方法是将 Authors 的架构设置为:
const AuthorscheR_474_11845@a = new scheR_474_11845@a({
id: String,publications: [scheR_474_11845@a.Type.Mixed]
},{ timestamps: true,_id: falsE});
然后在 findOneAndupdate 之前:
theNewObject.updatedAt: new Date();
所以你仍然可以使用:
publications.$[pub]": theNewObject
以上是大佬教程为你收集整理的MongoError:更新路径“publications.$[pub].updatedAt”会在“publications.$[pub]”处产生冲突全部内容,希望文章能够帮你解决MongoError:更新路径“publications.$[pub].updatedAt”会在“publications.$[pub]”处产生冲突所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。