试图更新文档中嵌套数组中的字段。
例如这里的示例模式。
childSchema = new Schema ( {
foo2 : String,
bar2 : String,
foobar : String
) };在此架构内
parentSchema = new Schema ( {
foo1 : String,
bar1 : String,
nested : [childSchema]
)};如果我想要基于bar2匹配的字符串更新foo2 (childSchema),我将如何做呢?
我试过以下几种方法
parentSchema.childSchema.updateOne( { 'nested.foo2' : 'string-that-matches' }, { $set: { 'nested.$.bar2' : 'new-string-for-bar2' } } )我通常会得到错误
TypeError: Cannot read property 'updateOne' of undefined我以前不喜欢,而不是,有这样的childSchema,它是用parentSchema制造的。它更多的是用来测试如何将它们分离出来。
如果我格式化这个问题的格式不对,我很抱歉,显然我试图从我的真实情况中做一个模拟模式集。我想这更像是我的查询,而不是设置。
谢谢!
发布于 2021-10-07 06:21:06
parentSchema已足够进行此更新。
parentSchema.updateOne( { 'nested.foo2' : 'string-that-matches' }, { $set: { 'nested.$.bar2' : 'new-string-for-bar2' } } )https://stackoverflow.com/questions/69475981
复制相似问题